php - Eloquent ORM multiple relationships -
php - Eloquent ORM multiple relationships -
there 3 tables.
users: - ... - some_param admins: - ... - club_id - some_param clubs: - id - title
each user can have multiple admins (related some_param), each admin can have multiple clubs, , want each club's title.
so defined relations:
class user extends eloquent { public function admins() { homecoming $this->hasmany('admin', 'some_param', 'some_param'); } } class admin extends eloquent { public function clubs() { $this->hasmany('club', 'id', 'club_id'); } }
and want utilize in template:
@foreach($user->admins $admin) @foreach($admin->clubs $club) {{ $club->title }} @endforeach @endforeach
but i'm getting error: relationship method must homecoming object of type illuminate\database\eloquent\relations\relation
@ line @foreach($admin->clubs $club)
.
what's doing wrong? give thanks in advance.
in next function:
public function clubs() { $this->hasmany('club', 'id', 'club_id'); }
you didn't return
utilize return
this:
public function clubs() { homecoming $this->hasmany('club', 'id', 'club_id'); }
also, when getting user
collection in $user
variable, seek utilize eager loading, example:
$user = user::with('admins.clubs')->find(1);
this cut down queries.
php laravel orm eloquent
Comments
Post a Comment