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" => "[email protected]"
"country_name" => "india"
]
1 => array:4 [▼
"id" => 2
"name" => "mehul"
"email" => "[email protected]"
"country_name" => "Dubai"
]
]
It will help you...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial