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

How to use join in Laravel eloquent query with example

Aman Jain, February 1, 2022November 5, 2023

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

laravel eloquent Join method by default use inner join. For example if you have 10 rows and you want the join from other table then it will only return the rows which satisfy both the tables.

Examples to use join in laravel

Laravel eloquent or query builder join method do the same as MySQL inner join function. Laravel 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')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();
//or

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

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

//

\DB::table('users')
        ->join('contacts', function ($join) {
         $join->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 join function in laravel eloquent.

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

Third example is advance join 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` inner 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` inner join `contacts` on `users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `32372328` inner join `orders` on `users`.`id` = `orders`.`user_id`

//or

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

Let’s understand laravel join in eloquent query with example

Example 1 – Laravel join function query using eloquent join method

So in this example i will use laravel query builder or eloquent join method to do the inner 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')
              ->join('contacts', 'users.id', '=', 'contacts.user_id') 
             ->join('orders', 'users.id', '=', 'orders.user_id')
             ->select('users.*', 'contacts.phone', 'orders.price')
             ->get();
        
   }
}

Output will be :

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

Example 2 – Advance join with multiple condition in join query

In this example i will use laravel advance 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')
              ->join('contacts', function ($join) {
                $join->on(function($query){
                    $query->on('users.id', '=', 'contacts.user_id')
                    ->orOn("contacts.phone",'users.phone');
                });
               })
             ->join('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` inner join `contacts` on (`users`.`id` = `contacts`.`user_id` or `contacts`.`phone` = `users`.`phone`) inner join `orders` on `users`.`id` = `orders`.`user_id`

In Laravel, you can also perform an inner join operation on two or more database tables using the Eloquent ORM (Object-Relational Mapping) or the Query Builder. An inner join combines rows from two or more tables based on a related column between them, and it only includes the rows that have matching values in both tables.

Here’s how to perform an inner join in Laravel:

  1. Using Eloquent ORM:

Assuming you have two Eloquent models representing your tables, let’s call them Model1 and Model2, and you want to join them based on a common column, such as column_id:

phpCopy code

$result = Model1::join('model2', 'model1.column_id', '=', 'model2.column_id') 
->select('model1.*', 'model2.some_column') 
->get();

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

Related

Php Laravel joinlaravelmysql

Post navigation

Previous post
Next post

Related Posts

Php How to compress and reduce image size while uploading in laravel

How to compress and reduce image size while uploading in laravel 8 ?

May 14, 2022May 14, 2022

While uploading the images in our application user can upload big size image but storing and rendering big size of image can reduce the performance of the application therefor its an important aspect to reduce the size of image without resizing it so we can keep the original size and…

Read More
Php File Validation in laravel 8

How to use file validation in Laravel 8 with example

November 27, 2021January 26, 2022

Laravel provides multiple ways to validate a form or a file in form with it’s own validation library. In this tutorial we will learn about the image validation for specific extension and file size. In this tutorial i will use a simple form and a file input field to validate…

Read More
Php How to Delete Cookies in Laravel

How to Delete Cookies in Laravel ?

July 26, 2022July 27, 2022

In this article we will learn to delete cookies in laravel. In our last article we learnt to set and get cookies and how cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In…

Read More

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 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • 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

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version