_Zaman
1 year ago
257
20
Laravel

display pivot table multiple data into blade

Posted 1 year ago by _Zaman

Hi, I have a user table, meeting table, and a pivot table meeting_user. I need to show meeting table data and pivot table data. I can display normal foreign key data using the relationship method like @foreach($meetings as meeting) <td>{{$meeting->user()->name }} where I am sending all meeting table data using $meetings = Meeting::all() But now I am storing multiple users so how can I show the name of those users that is stored in meeting_user table where the userType == 'host'. I have a field isHost that is a boolean type and 1 means it is host type user and 0 means it is visitor type user.

User Model

public function meetings()
    {
        return $this->belongsToMany('App\Meeting')->withPivot('meeting_id', 'user_id');
    }

Meeting Model

public function users(){

        return $this->belongsToMany('App\User')->withPivot('meeting_id', 'user_id');
    }
   <tr>
    @if( )
    <td>{{ $meeting->users()->name}}</td>
    @endif
    <td>{{ $meeting->startDate}}</td>
        <td>{{ $meeting->endDate}}</td>                      
    <td>{{ $meeting->locations->name}}</td>
                                
</tr>

Please sign in or create an account to participate in this conversation.