ItSolutionStuff.com

Laravel Query Builder Where Exists Example

By Hardik Savani β€’ November 5, 2023
PHP Laravel MySql

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:

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..

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Sum Query with Where Condition Example

Read Now β†’
β˜…

Laravel Group By with Min Value Query Example

Read Now β†’
β˜…

Laravel Group By with Max Value Query Example

Read Now β†’
β˜…

Laravel Case Sensitive Query Search Example

Read Now β†’
β˜…

PHP Laravel Left Join Query Example

Read Now β†’
β˜…

PHP Laravel Inner Join Query Example

Read Now β†’
β˜…

Laravel Eloquent whereNotNull() Query Example

Read Now β†’
β˜…

Laravel Eloquent whereNotBetween() Query Example

Read Now β†’
β˜…

Laravel Eloquent whereBetween() Query Example

Read Now β†’
β˜…

Laravel Eloquent Where Query Examples

Read Now β†’
β˜…

How to Create and Use Query Scope in Laravel Eloquent

Read Now β†’
β˜…

How to use Union Query with Laravel Eloquent?

Read Now β†’
β˜…

How to Get Query Strings Value in Laravel?

Read Now β†’