How to Use Inner Join In Laravel 9?

Hello Dev, Today we are going to learn on How to Use Inner Join In Laravel 9? This tutorial will cover on how you can use Inner Joins in latest laravel 9 application.

We will implement a how to apply inner joins in laravel 9. let’s discuss about how to make inner joins in laravel 9. if you want to see example of how to write inner joins in laravel 9 then you are a right place. Follow bellow tutorial step of laravel 9 inner joins query builder.

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

Also Read: Laravel 9 Send Web Push Notification with Firebase Example

Example on How to Use Inner Join In Laravel 9?:

users Table:

Users Table - Inner Join Laravel Example
Users Table – Inner Join Laravel Example

countries Table:

Countires Table – Inner Join Laravel Example

Also Read: Laravel 9 Vue Js Roles and Permissions + Vite Js

Laravel Query:

<?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);
    }

}

Output:

array:2 [▼
  0 => array:4 [▼
    "id" => 1
    "name" => "keval"
    "email" => "kevalkashiyani9@gmail.com"
    "country_name" => "india"
  ]
  1 => array:4 [▼
    "id" => 2
    "name" => "mehul"
    "email" => "mehulbagada@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 Stripe Payment Gateway Integration Example

1 Comment

Leave a Reply