Laravel 9 Join Query Example Tutorial

10-Apr-2023

.

Admin

Laravel 9 Join Query Example Tutorial

This post is focused on how to use inner join in laravel 9. We will use how to apply inner join in laravel 9. We will look at example of how to make inner join in laravel 9. In this article, we will implement a how to write inner join in laravel 9. you will do the following things for laravel 9 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.

So, let's see bellow example:

Example


users Table:

countries Table:

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"

]

]

It will help you...

#Laravel 9