Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to use left join in Laravel Eloquent

How to use left join in Laravel eloquent query with example

Aman Jain, February 5, 2022October 4, 2022

In this article we will learn to use left join in laravel eloquent and query builder. Laravel itself provide inbuilt method to left join the table like in MySQL or SQL based database we can do join multiple tables using leftJoin function.

For example if you have 10 rows and you want the join from other table then it will return the all rows from left table and the matching records from the right table.

Laravel eloquent or query builder leftJoin method do the same as MySQL left join function. Laravel left join accepts multiple parameters to the function and first parameter as table name and rest are columns constraints.

Here is the syntax

DB::table('users')
            ->leftJoin('contacts', 'users.id', '=', 'contacts.user_id')
            ->leftJoin('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
//or

Model::select('users.*', 'contacts.phone', 'orders.price')
->leftJoin('contacts', 'users.id', '=', 'contacts.user_id')
->leftJoin('orders', 'users.id', '=', 'orders.user_id')
->get(); // or first() 

//or
 
 Model::select('users.*', 'contacts.phone', 'orders.price' )
->leftJoin('contacts', function ($leftJoin) {
            $leftJoin->on('users.id', '=', 'contacts.user_id')->On('`contacts`.`phone`','');
 })

//

\DB::table('users')
        ->leftJoin('contacts', function ($join) {
         $leftJoin->on(function($query){
            $query->on('users.id', '=', 'contacts.user_id')
           ->orOn("contacts.phone",'users.phone');
         });

In the above syntax we have used 3 examples to show all possibilities to use leftJoin function in laravel eloquent.

First is using the Query Builder DB class and leftJoin method of eloquent. Second is Model with leftJoin function on eloquent.

Third example is advance leftJoin to add multiple conditions to join in laravel and the final one to use grouping in laravel join.

Result of above query will be:

select `users`.*, `contacts`.`phone`, `orders`.`price` from `users` left join `contacts` on `users`.`id` = `contacts`.`user_id` inner join `orders` on `users`.`id` = `orders`.`user_id`

//or

select `users`.*, `contacts`.`phone`, `orders`.`price` from `users` left join `contacts` on `users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `32372328` left join `orders` on `users`.`id` = `orders`.`user_id`

//or

select `users`.*, `contacts`.`phone`, `orders`.`price` from `users` left join `contacts` on (`users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `users`.`phone`) left join `orders` on `users`.`id` = `orders`.`user_id`

Let’s understand laravel leftJoin in eloquent query with example

Example 1 – Laravel left join function query using eloquent leftJoin method

So in this example i will use laravel query builder or eloquent leftJoin method to do the left join. Simple join to execute join between the two tables

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        \DB::table('users')
              ->leftJoin('contacts', 'users.id', '=', 'contacts.user_id') 
             ->leftJoin('orders', 'users.id', '=', 'orders.user_id')
             ->select('users.*', 'contacts.phone', 'orders.price')
             ->get();
        
   }
}

Output will be :

select `users`.*, `contacts`.`phone`, `orders`.`price` from `users` left join `contacts` on `users`.`id` = `contacts`.`user_id` left join `orders` on `users`.`id` = `orders`.`user_id`

Example 2 – Advance join with multiple condition in leftJoin query

In this example i will use laravel advance left join to create multiple conditions in join

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       \DB::table('users')
              ->leftJoin('contacts', function ($leftJoin) {
                $leftJoin->on(function($query){
                    $query->on('users.id', '=', 'contacts.user_id')
                    ->orOn("contacts.phone",'users.phone');
                });
               })
             ->leftJoin('orders', 'users.id', '=', 'orders.user_id')
             ->select('users.*', 'contacts.phone', 'orders.price')
             ->get()
   }
}

In the above example we have used users, contacts and orders table to create the join and also we have used multiple condition in join.

To make the grouping we have used closure functions.

Output will be :

select `users`.*, `contacts`.`phone`, `orders`.`price` from `users` left join `contacts` on (`users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `users`.`phone`) left join `orders` on `users`.`id` = `orders`.`user_id`

Also Read : How to use join in Laravel eloquent query with example

Share this:

  • Facebook
  • X

Related

Php Laravel laravelleft joinmysql

Post navigation

Previous post
Next post

Related Posts

Php How to drop foreign key in laravel migration

How to drop foreign key in laravel migration ?

September 28, 2022November 8, 2023

Drop foreign key to existing table are easy as creating a foreign key and adding columns to it. In laravel migration we can drop foreign key in laravel migration to existing table using dropForeign method of migration class. Major difference between creating new table and updating new table is Schema::create…

Share this:

  • Facebook
  • X
Read More
Php install laravel composer

How to install laravel 8 ?

September 20, 2021May 6, 2022

Laravel is most popular framework of PHP. It provides lots of in-build tools and library to build the web application. Laravel is used to to create robust custom web applications with ease of amazing developer experience. Laravel is easily customizable and provides tools like ORM, Validation libraries, Queue, Mail, Routing,…

Share this:

  • Facebook
  • X
Read More
Php Get Json Post Data in Laravel

How to Get Json Post Data in laravel from Request ?

June 8, 2022November 9, 2023

In this article we will learn to get json post data in laravel from request. Laravel by default supports for form-data and x-www-form-urlencode which we can get easily using the Request class as follow $request->field_name or $request->get(‘field_name’). To retrieve the json post data in laravel we need to call json…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version