the error displaying is clear, you don't have a METHOD called Cse\Models\Role() .
in the ->with() you have to pass the name of the method, no the namespace of the model. Like this:
$userRoles = $this->userRoles()->with('role')->get();
I think you have an method called role() or what ever you want, defining the relation with Cse\Models\Role model, same way yo have userRoles(). But I am not sure what are you doing.
Here some pseudo-code.. if you are trying to get users with roles:
class user extends eloquent{
public function roles()
{
return $this->hasMany('Namespaces\Role','user_id');
}
}
$usersWithRoles = $this->with('roles')->get(); // you get the user and the related roles foreach(usersWithRoles as $ur)
$ur->role_name
endforeach
next one is different result
$rolesOfTheUser = $this->roles()->orderBy('role_name')->get(); //returns the roles that the user has.
Yes you are right, I have method UserRole::role() and that's what's called when ->with() is executed. I just expected that ->with() can perform automatical joins on model class names, it seems I was wrong. The error is clear, however Eloquent probably can figure out that I am passing model class, not model method there. Or maybe not.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community