In this video we are going to learn about joins clauses and also learn about how to improve the performance of your laravel application.
Why we use join clause in laravel.
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
There are serveral type of join clauses used in laravel.
like inner join clause, left join clause, right join clause, cross join clause, advanced join clause and Sub query join clause.
In this video we will discuss about inner, left and right join.
Okay, so lets start with inner join clause.
Before starting the join lets create a table in our database.
For that go to the phpmyadmin.
Now click on laravel8prodb database.
Inside this database lets create two table one users table and another which is posts table.
So lets run the following sql query in phpmyadmin.


CREATE TABLE `users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`phone` varchar(15) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



CREATE TABLE `posts` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`body` text NOT NULL,
`user_id` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


Now insert some users inside the user table and also insert some posts in posts table.
Now lets create join so switch to the project.
Lets see inner join first.
Laravel Inner Join clause selects records if the given column values matching in both tables.
Now we have two tables users and posts.
Both have id columns, and there is a user_id in the post, which is a foreign key for the users' table.
Alright so for creating inner join.
Lets create a function.


public innerJoinCluase()
{
$result = DB::table('users')
->join('posts', 'users.id', '=', 'posts.user_id')
->select('users.name', 'posts.title', 'posts.body')
->get();
return $result;
}


Alright now create a route for this function.
so just go to the web.php.
and here.
Type.


Route::get('/inner-join',[PostController::class , 'innerJoinCluase');


Save the file and lets check.
Switch to the browser and go to the url /inner-join.
You can see here user name , post title, and post body.
These records are coming from both the tables.
Now lets see the left joins.
The Laravel LEFT JOIN clause returns all rows from the left table, even if there are no matches in the right table, the result is NULL from the right side.
Lets create a function for the left join inside the PostController.

public function leftJoinClause()
{
$results = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
return $results;
}

And now create route.


Route::get('/left-join','PostController@leftJoinClause');


Now lets check it so go to the url /left-join.
You can see the result here.
Now lets see Right Join.
The Laravel Right JOIN clause returns all rows from the right table, even if there are no matches in the left table, The result is NULL from the left side. now create function for right join.
So goto the PostController.
Just type here.

public function rightJoinClause()
{
$resutls = DB::table('users')
->rightJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
return $resluts;
}


Now create the route so go to the web.php file and create the route.


Route::get('/right-join','PostController@rightJoinClause');


Now lets check the right-join.
So go to the url /right-join.
You can the result here all records are coming from right table which is posts table.
So in this way you can use the join clause in laravel 8.