Laravel 8 Inner Join Query Example

Websolutionstuff | Nov-24-2021 | Categories : Laravel PHP MySQL

In this tutorial we will learn about laravel 8 inner join query example. Also see how to join two tables in laravel 8. In laravel you can use group by query same as PHP. So, we will also see query of laravel inner join with 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.

So, let's start example of inner join in laravel 8.



SQL Query:

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

SELECT column_name(s)
FROM table1
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 fileds then you can use * for select all data.

public function index()
	$users = User::select('', '', '', ' as country_name')
        	->join('countries', '', '=', 'users.country_id')

 Using DB:

use Illuminate\Support\Facades\DB;

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


Subquery Joins with groupBy()

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)



Laravel Join() with 3 Tables

Now, I will give example about join 3 table in laravel and all tables are connected with each other.

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


You might also like :

Recommended Post
Featured Post
Laravel 9 Has Many Through Relationship Example
Laravel 9 Has Many Through Rel...

In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is diffic...

Read More


Laravel 9 Refresh DataTable Without Reloading Page
Laravel 9 Refresh DataTable Wi...

In this article, we will see laravel 9 refresh datatable without reloading the page. When I have a data table that...

Read More


Bootstrap DateTimePicker Example
Bootstrap DateTimePicker Examp...

In this post i will show you how to implement bootstrap datetimepicker with example, bootstrap 4 datetimepicke...

Read More


Laravel 8 QR Code Generator Example
Laravel 8 QR Code Generator Ex...

In this tutorial, we will see the laravel 8 QR code generator example. we will generate QR code using simpleso...

Read More