Laravel 9 Inner Join Query Example

Websolutionstuff | Mar-30-2022 | Categories : Laravel PHP MySQL

In this article, we will see laravel 9 inner join query example. Also, see how to join two tables in laravel 9. In laravel 9 you can use group by query same as PHP. So, we will also see query of laravel 9 inner join with the group by. The query builder may also be used to add join clauses to your queries. To perform a basic "inner join", you may use the join method on a query builder instance.

For laravel inner join query example we need the first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. You may even join multiple tables in a single query and inner join query use in laravel 6, laravel 7, laravel 8, laravel 9. 

So, let's see an example of inner join in laravel 9, laravel 9 join two tables, inner join in laravel 9, laravel inner join group by, inner join query in MySQL, inner join query in PHP, subquery joins in laravel 9, laravel 9 join query, join in laravel 9.

laravel_9_inner_join

 

SQL Query :

In this example, we will create a users table and countries table. we will add the country_id  foreign key on the user's table. So, when I get users at that time we will get the country name from country_id using an inner join.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

 

 

select `users`.`id`, `users`.`name`, `users`.`email`, `countries`.`name` as `country_name` 
from `users` 
inner join `countries` on `countries`.`id` = `users`.`country_id`

 

Laravel Query :

In this example, select data like id, name email, and country name but if you want all fields then you can use * to select all data.

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

 Using DB:

use Illuminate\Support\Facades\DB;

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

 

Subquery Joins with groupBy() Function

In this example, we will retrieve a collection of users where each user record also contains the created_at timestamp of the user's most recently published blog post.

$posts = DB::table('posts')
                   ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
                   ->where('is_published', true)
                   ->groupBy('user_id');

 

 

Laravel Join() with 3 Tables

Now, I will give an example of joining 3 tables in laravel and all tables are connected with each other.

$users = User::join('posts', 'posts.user_id', '=', 'users.id')
              ->join('comments', 'comments.post_id', '=', 'posts.id')
              ->get(['users.*', 'posts.descrption']);

 


You might also like :

Recommended Post
Featured Post
How to Use Bitmasks for Efficient Data Filtering?
How to Use Bitmasks for Effici...

Data filtering might not sound like the most thrilling topic, but when it comes to processing large volumes of informati...

Read More

Oct-25-2023

How To Create List And Grid View Using JavaScript
How To Create List And Grid Vi...

In this article, we will see how to create a list and grid view using javascript. many times clients have requirements l...

Read More

Dec-23-2020

How To Add Toastr Notification In Laravel 10
How To Add Toastr Notification...

In this article, we will see how to add toastr notification in laravel 10. Here, we will learn about toastr notification...

Read More

Mar-06-2023

7 Tips and Tricks for Laravel Migration
7 Tips and Tricks for Laravel...

As a developer who has been deeply immersed in the Laravel ecosystem, I've come to appreciate the power and flexibil...

Read More

Oct-30-2023