The database query builder in Laravel provides a simple, intuitive interface for generating and performing database queries. It works with every one of Laravel’s supported database systems and may be used to conduct most operations on the database in your application.
Today, in this post, we will show you an example of a laravel query builder where exists. When using the where exists clause in SQL Query in Laravel. And we can utilize SQL where exists clause in our laravel project thanks to whereExists.
As a result, it is incredibly simple to use and learn. In the where condition, we can use the SELECT query.
We can learn how to utilize whereExists in our application by looking at the example code below.
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:
Read Also: Laravel Where Clause with Function Query Example
Item::query()
->whereExists(function ($query) {
$query->select("items_city.id")
->from('items_city')
->whereRaw('items_city.item_id = items.id');
})
->get();
I hope it can help you..