Laravel Left Join Where Null Condition Example

Laravel 8 , Laravel 7 , Laravel

Nicesnippets

558

19-05-2021


Laravel Left Join Where Null Condition Example

Laravel is so easy to use.so you can just follow my step by step and learn Left Join Where Null Condition Laravel.

In this tutorial, you will learn what is laravel left join and how to use left join in laravel. And As well as how to use laravel left join with multiple where conditions.

Note that, data has to be fetched from more than one table. At that time the joins are used in laravel.

Here i will give you two example of left join where null condition laravel fisrt one is sql query and second laravel eloquent query. So let's see the bellow example:

SQL Query:


SELECT

*

FROM

customers

LEFT JOIN

orders

ON customers.id = orders.customer_id

WHERE

orders.customer_id IS NULL

But the laravel query is so easy and short.

Laravel Eloquent Query:

<?php

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$data = Customer::leftJoin('orders', function($join) {

$join->on('customers.id', '=', 'orders.customer_id');

})

->whereNull('orders.customer_id')

->get();

dd($data);

}

It will help you....


Recommended Posts