Home Laravel Laravel Query Builder Where Exists Example

Laravel Query Builder Where Exists Example

by Shahriar Sagor

Today now on this post i will show you laravel query builder where exists example. When we are use sql where exists clause in laravel. And whereExists through we can use sql where exists clause in our laravel project. So It is very easy to use and we can easily understand. we can give SELECT statement in where condition. we can see the bellow example and we can learn how to use whereExists in our application.

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();

Read Also :Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us onĀ facebook.

close

You may also like