Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smithdp1's avatar

Correct pivot relationship Laravel 4.2

i have 3 tables:

representatives - id, name

teams - id, name

representative_team - representative_id, team_id, leader:boolean

My Models:

class Representative extends \Eloquent {
 
public function team(){

        return $this->belongsToMany('Team', 'representative_team')->withPivot('leader');
    }
}

class Team extends \Eloquent {
   
public function representatives(){

        return $this->belongsToMany('Representative')->withPivot('leader');
    }
}

What I am trying to do is: if a representative is a leader, loop through and show members on his team. When I try this in view i get errors:

@if($represntative->team->leader)

guess i am stuck and the reason for asking for help. Thx!

0 likes
9 replies
bestmomo's avatar

There is a typo :

@if($representative->team->leader)

What are the errors ?

smithdp1's avatar

hey thx for quick response and catching typo!
error:

Undefined property: Illuminate\Database\Eloquent\Collection::$leader (View: /var/www/clients/client1/web2/web/app/views/repoffice/teamleaders/index.blade.php)

here is how I am testing in view:

@if($representative->team->leader)
yes
@else
no
@endif 

I have also tried:

@if($representative->team->pivot->leader)

error:

Undefined property: Illuminate\Database\Eloquent\Collection::$pivot (View: /var/www/clients/client1/web2/web/app/views/repoffice/teamleaders/index.blade.php)
smithdp1's avatar

i can also echo this in view:

{{$representative->team}}

and get this:

[{"id":"6","name":"Florida","created_at":"2015-04-30 03:55:38","updated_at":"2015-04-30 03:55:38","pivot":{"representative_id":"391360","team_id":"6","leader":"1"}}]
bobbybouwmann's avatar

Well as you can see pivot is an object as well, I guess you can do this to get the leader

$representative->team->pivot->leader
smithdp1's avatar

@bobbybouwmann yes that's what I thought too...but no dice. If i try:

@if($representative->team->pivot->leader)
yes
@else
no
@endif 

i get error:

Undefined property: Illuminate\Database\Eloquent\Collection::$pivot (View: /var/www/clients/client1/web2/web/app/views/repoffice/teamleaders/index.blade.php)

I am completely stumped!!!

bobbybouwmann's avatar
Level 88

Oow wait, team is a collection here

@foreach(@if($representative->team as $team)
    @if $team->pivot->leader
        Yes
    @else
        No
    @endif
@endforeach

You probably want to update the way you retrieve the team! Maybe you can show that, so we can help you with that

smithdp1's avatar

@bobbybouwmann ...thats it your right! man pulling my hair out. Here is how I am getting team:

class Representative extends \Eloquent {
 
public function team(){

        return $this->belongsToMany('Team', 'representative_team')->withPivot('leader');
    }
}
bobbybouwmann's avatar

This is a many to many relation so you will get a collection back. If you only want one result I need to see your current query

smithdp1's avatar

@bobbybouwmann - Buddy you got me straightened out now...sometimes things get cloudy...but sun is shinning now.

Thank you for all your help!

Please or to participate in this conversation.