Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to chain eloquent relations in laravel?

So far I was extracting the relation objects as arrays and then doing something like:

App\Model::find($id)

But however is there a way to do something like:

Auth::user()->group()->members()

It works until Auth::user()->group but no further chaining. Please help if you've done something. Or I'm just newbie.

like image 247
Leap Hawk Avatar asked Oct 15 '16 09:10

Leap Hawk


People also ask

How to manage relationships in Laravel?

There are three basic types of relationships one-to-one, one-to-many and many-to-many. In custom PHP or in frameworks that work on basis of Active Record pattern you have to manage the relations using joins while in Laravel you have that option too but you can manage all those relations in a very simple way using eloquent models.

What is Laravel eloquent and how to use it?

Ralph J. Smit Laravel & PHP-developer. Laravel Eloquent is one of the flagship features of the Laravel framework. In part that is also thanks to its awesome support for defining, creating and managing relationships between different tables of data.

What are eloquent database relationships?

Database tables are often related to one another. For example, a blog post may have many comments or an order could be related to the user who placed it. Eloquent makes managing and working with these relationships easy, and supports a variety of common relationships: Eloquent relationships are defined as methods on your Eloquent model classes.

How do I use eloquent to retrieve specific columns of relationships?

For this reason, Eloquent allows you to specify which columns of the relationship you would like to retrieve: When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve. Sometimes you might want to always load some relationships when retrieving a model.


1 Answers

You could use eager loading to load the user's group and then load all of the members of that group.

$user = User::with(['group', 'group.members'])->find(1);

// OR if you already have a user object (Like when using the Auth facade)
$user = Auth::user()->load(['group', 'group.members']);

foreach ($user->group->members as $member) {
    // Do something with a member
}

However, if you essentially want to jump down the structure a level, and get all the members related to a user, you could use the hasManyThrough relationship, in that a user has many members, through a group.

// In your User model
public function members()
{
    return $this->hasManyThrough(Member::class, Group::class);
}

That way you can simply access the members directly through the user:

$members = Auth::user()->members;

Instead of doing a query to access the user's group and then doing another query to access that group's members, Laravel would use a single query with a join to get you the members.

Take a look at the hasManyThrough relationship here

like image 76
Jonathon Avatar answered Sep 28 '22 17:09

Jonathon