We’ll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database tables by establishing relationships between them.
Laravel’s Eloquent ORM (Object-Relational Mapping) is an ActiveRecord implementation that simplifies database communication with PHP applications. It allows developers to interact with database tables using PHP objects and methods, which eliminates the need to write complex SQL queries manually in the application.
We’ll discuss the following Laravel join methods:
Inner Join
: Returns only the matching rows from both tables.Left Join
: Returns all rows from the left table and the matching rows from the right table.Right Join
: Returns all rows from the right table and the matching rows from the left table.Full Outer Join
: Returns all rows when there is a match in either the left or right table, It returns NULL values where there’s no match.Cross Join
: Returns all rows from both tables.Let’s assume you have an “employees” and a “departments” table, and you want to perform a join operation using Laravel’s Eloquent ORM. Define the relationship between Employee and Department models:
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Employee extends Model { public function department() { return $this->belongsTo(Department::class); } } class Department extends Model { public function employees() { return $this->hasMany(Employee::class); } }
Here’s a foreign key department_id
in the employees table referencing the id
column in the departments table.
There are the following types of join available in Laravel ORM. Let’s discuss them one by one with examples.
An inner join in Eloquent fetches records that have matching values in both tables. It filters out non-matching records from both table’s data.
$employees = Employee::join('departments', 'employees.department_id', '=', 'departments.id') ->select('employees.*', 'departments.name as department_name') ->get();
A left join retrieves all records from the left table and matching records from the right table. It includes all records from the left table, regardless of whether corresponding matches are in the right table.
$employees = Employee::leftJoin('departments', 'employees.department_id', '=', 'departments.id') ->select('employees.*', 'departments.name as department_name') ->get();
A right join fetches all records from the right table and matching records from the left table. It includes all records from the right table, regardless of whether there are corresponding matches in the left table.
$employees = Employee::rightJoin('departments', 'employees.department_id', '=', 'departments.id') ->select('employees.*', 'departments.name as department_name') ->get();
A cross join returns the Cartesian product of two tables, meaning it combines each row from the first table with every row from the second table.
$employees = Employee::crossJoin('departments') ->select('employees.*', 'departments.name as department_name') ->get();
Laravel does not support full outer join but you can achieve this by combining a left join and a union.
use App\Models\Employee; use Illuminate\Support\Facades\DB; $fullOuterJoinResults = Employee::leftJoin('departments', 'employees.department_id', '=', 'departments.id') ->select('employees.*', 'departments.name as department_name') ->unionAll(Employee::rightJoin('departments', 'employees.department_id', '=', 'departments.id') ->select('employees.*', 'departments.name as department_name')) ->get();
We have covered Laravel eloquent join methods with examples. We have discussed left join, right join, cross join, outer join and inner join methods. You can use any of them as per your requirements.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More