In this tutorial we will talk about how to make laravel 9 join query with example. We will see how to apply inner join in laravel 9. We will look at below code example of how to make inner join query in laravel 9. In this tutorial, we will execute a how to write inner join in laravel 9. you will do the given things for laravel 9 inner join query builder.
In this code snippet, We will generate countries table and users table. We will add country_id on users table and add countries table id on users table. So when we get users, We will get country name by using country_id using inner join.
So, let’s see bellow example:
Create user table and get user data
select * from users
After that, create country table and get all countries
select * from countries
Here the 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);
}
}
Outout
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"
]
]
0 Comments