Thursday, December 26, 2024
Google search engine
HomeLanguagesLaravel Right Join Example Tutorial

Laravel Right Join Example Tutorial

Laravel right join example; In the previous tutorial, you have learned about laravel left join with examples. If you are not reading to previous laravel left join tutorial, you can check out this Laravel left join example post. Because it is completely opposite to the laravel left join.

In this Laravel right join query example tutorial, you will learn what is laravel right join and how to use right join in laravel. And As well as how to use laravel right join with multiple where conditions.

Laravel Right Join Examples

The Laravel Right JOIN eloquent 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.

Let’s understand of laravel right join, suppose you have two 2 tables in the database. One is a users table and the other is an city table.

And when you get the data from the users table. And right join with city table. At this time, The query returns all rows in the city table and all matching rows found in the users table.

Here, take a look at examples of laravel right join query, see the following examples one by one:

Example 1: Laravel Right Join Query

Here, this example shows how to fetch data using laravel right join, you can see the following example:

User::rightJoin('city','city.user_id','=','users.id')
          ->select('users.name','city.city_name')
         ->get();

When you dump the above-given laravel right join query, you will get the following SQL query:

select `users`.`name`, `city`.`city_name` from `users` 
right join `city` on `city`.`user_id` = `users`.`id`

Example 2: Laravel Right Join with Multiple Condition

In this example shows how to fetch data using laravel right join with multiple where clause conditions, you can see the following example:

User::rightJoin('city','city.user_id','=','users.id')
          ->select('users.name','city.city_name')
          ->where('users.status', 'active')
          ->where('city.status', 'active')
         ->get();

When you dump the above-given laravel right join with multiple conditions query, you will get the following SQL query:

select `users`.`name`, `city`.`city_name` from `users` 
right join `city` on `city`.`user_id` = `users`.`id` where `users`.`status` = active and `city`.`status` = active

Conclusion

Laravel right join tutorial, you have learned how to use laravel right join and as well as how to use right join with multiple conditions.

More Laravel Posts

Recommended:-Laravel Try Catch
RELATED ARTICLES

Most Popular

Recent Comments