Share this:

Today we are going to learn How to Use Inner Join In Laravel 9. This tutorial will cover on how can you use inner joining in laravel 9 application.

I will let you know example of how to use inner join in laravel. In this article, we will implement a how to apply inner join in laravel application. let’s discuss about how to make inner join in application. if you want to see example of how to write inner join in application then you are a right place. Follow bellow tutorial step of laravel application inner join query builder.

In this example, I will create users table and countries table. I will add country_id on users table and add countries table id on users table. so when I get users at that time we will get country name from country_id using inner join.

Also Read: Laravel 9 User Roles and Permissions Tutorial Example

Example of How to Use Inner Join In Laravel 9:

Tables:

Let know the table fields in the user table. As you can seen from the below image there are id, name, email, password etc. fields.

Users Table:

Users Table - How to Use Inner Join In Laravel 9
Users Table

Let know the table fields in the countries table. As you can seen from the below image there are id, name, created_at, updated_at fields.

Countries Table:

Countries Table - How to Use Inner Join In Laravel 9
Countries Table

Also Read: Laravel 9 Vue JS Form Validation Example

Laravel Query:

The below query will show you how to use inner join in laravel.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class JoinController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $users = User::select(
                            "users.id", 
                            "users.name",
                            "users.email", 
                            "countries.name as country_name"
                        )
                        ->join("countries", "countries.id", "=", "users.country_id")
                        ->get()
                        ->toArray();
  
        ($users);
    }
}

Also Read: Laravel Vue 3 File Uploading with Progress Bar using Vite Example

Output:

This is the output of the above query which we had run. The Above query will return an array of lists as shown below.

array:2 [▼
  0 => array:4 [▼
    "id" => 1
    "name" => "laraveltuts"
    "email" => "admin@laraveltuts.com"
    "country_name" => "india"
  ]
  1 => array:4 [▼
    "id" => 2
    "name" => "nitin"
    "email" => "pujarinitin92@gmail.com"
    "country_name" => "dubai"
  ]
]

Conclusion:

Today, We had learn How to Use Inner Join In Laravel 9. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel 9 Livewire Multi Step Form Wizard Tutorial

Share this:

Categorized in: