Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

It does not work that way. You most likely(I may be wrong) can't pass usersGroup.group.name to the query as this way you will try to make statement like SELECT x,y,z, usersGroup.group.name FROM users which will fail.

I don't see the point of creating UserGroup model. Instead use belongsToMany relationship like:

//User

public function groups(){
    return $this->belongsToMany('Group', 'users_groups', 'user_id', 'group_id');
    //first parameter is model name, second is pivot table, third is foreignKey column name and 4th is localKey column name
}

This way when you call $user->groups()->get() you will get all the Group models that user belongs to through the pivot table.

0

Thanks for the advice! Haven't played with pivot tables as of yet.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

maltex maltex Joined 18 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.