Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devamit2018's avatar

Eloquent Relationship with multiple left joins

How can i make realtionship with multiple left joins ?like example LEFT JOIN users u ON Students.ID = u.user_id AND Students.is_deleted = u.is_deleted AND Students.client_id = U.client_id WHERE u.fiscal_year = 2022 It works with query builder but want to achieve it with eloquently

0 likes
2 replies
vincent15000's avatar

Hello,

I'm not sure to understand what you need. In your query you ned only one left join but you have multiple conditions.

The Query Builder helps you to write the query and Eloquent helps you writing the relationships between the different models. And your question seems to ask how it is possible to write the query with Eloquent ?

So I have a question : do you need help to write the query with the query builder or to write the relationships in your models ?

Query Builder

$result = Student::join('users', 'users.id', '=', 'students.id')->...->get();

Eloquent

// In the User model

public function student()
{
	return $this->hasOne('App\Models\Student');
}

// In the Student model

public function user()
{
	return $this->belongsTo('App\Models\User');
}

Please or to participate in this conversation.