Laravel Eloquent Join

[亡魂溺海] 提交于 2019-12-07 07:45:30

In your Member model declare the following relationship method:

public function club()
{
    return $this->belongsTo('Club');
}

So now you can query all members with their Club info as:

$members = Member::with('club')->get();

In your View when looping the Members you may try something like this:

@foreach($members as $member)
    {{ $member->name }}
    {{ $member->club->name }}
@endforeach

This is not Eloquent Join but using it's relationship technique you may do it as given above, if you want to join then you may try something like this:

$members = Member::join('clubs', 'members.club_id', '=', 'clubs.id')
                 ->select('members.*', 'clubs.name as club_name')
                 ->get();

In this case, you may loop and use the Club info like this;

@foreach($members as $member)
    {{ $member->name }}
    {{ $member->club_name }}
@endforeach
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!