Laravel Eloquent Query | Laravel Query Builder Where Exists Example

--

you use SQL where exists clause in laravel. “whereExists” through you can use SQL where exists clause in your laravel project. It is very easy to use and you can easily understand. You can give a SELECT statement in where condition. you can see the below example and you can learn how to use whereExists in your app.

SQL Query

SELECT *
FROM `items`
WHERE EXISTS
(SELECT `items_city`.`id`
FROM `items_city`
WHERE items_city.item_id = items.id)

Using Laravel Query Builder

DB::table('items')
->whereExists(function ($query) {
$query->select("items_city.id")
->from('items_city')
->whereRaw('items_city.item_id = items.id');
})
->get();

--

--

No responses yet