In Laravel, how can I access data from a pivot table?

luisfer

Maybe I'm implementing wrong. Maybe I'm doing things I'm not supposed to do, but conceptually I think I'm okay.

I have two models, one is Users, other is Communities, and since it is a many to many relationship I need an intermediate table, Laravel calls them a pivot table. According to Laravel recommendations it is named communities_users

Normally, the pivot tables only contain the id's to the main tables. But I need a bit more data. Check this image, the migrations are done to match this diagram:

enter image description here

Here I have a role in the pivot table. Lets say a user belongs to two communities, one as community's president, other as a member.

Of course the president will have more capabilities than the member, for example, he can accept a pending person to become a member person, or he can promote a member to a deputy status or so...

Now lets say I want to get the names of the people with deputy role on a given community. I can easily do that using the Query Builder, by joining the three tables and applying where('communities_users.role', 'deputy'),

Now, what if I want to use the ORM and get the whole User model?

I'd like to have a function on the Community so it gives me the users of a given role, maybe:

public function users($role) {
    ...
}

So I can do things like:

$community = Community::find($id);
foreach ($community->users('deputy') as user) {
    ...
    echo user->name; // or whatever
    ...
}

Is it possible? How?

Aken Roberts

Hiding under the Many To Many section of the Eloquent relationship docs:

Filtering Relationships Via Intermediate Table Columns

You can also filter the results returned by belongsToMany using the wherePivot and wherePivotIn methods when defining the relationship:

return $this->belongsToMany('App\Role')->wherePivot('approved', 1);

return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);

Assuming your Communities model already has a belongsToMany relationship set up for Users, you should be able to do something like:

$deputies = $community->users()
    ->wherePivot('role', 'deputy') // or equivalent value
    ->get();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel - Can't access pivot table data

Laravel: How do I get data from this pivot table?

How to limit access from pivot table? Laravel

laravel how can i show the pivot: data from the get() method

how can i access to pivot table fields

How can I access selected report filter values in Excel Pivot table from VBA code or Formulas?

Can't get all data from Pivot Table in Laravel

how can I get the "value" field from the pivot table (product to "value")? -Laravel

Laravel Eloquent How can I select and manipulate rows in pivot table?

How can I write a relation for pivot table in Laravel?

How to Retrieve data from Pivot table in Laravel using Datatables

how to delete data from two pivot table in same time in laravel

How I can check that unique data in pivot table already exists?

Access Data From Pivot Table On Model

Get data from pivot table in view laravel

Laravel 5.7 Get data from Pivot Table

accessing data from a pivot table in laravel

Laravel what does the session table column "payload" contain, and how can I access this data?

laravel - How to get data by pivot table

how to save many data in pivot table in laravel

how can I fetch data in data from 2 table using with hasOne relation in laravel model

How to access a field data from users table from a controller in Laravel?

Laravel: How to get records from a pivot table?

I can not access the array that I attached to my pivot table?

In Access, how can I pull data from another table by matching a field?

How can I create pivot table in SAS?

How can I group values on a Pivot Table?

Laravel - Access additional column data within a pivot table

How can I link and access other table after logging in (laravel)