Laravel Query Builder Where Exists Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

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.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →