如何在Laravel上查看多对多表

如何在Laravel上查看多对多表,laravel,Laravel,我有三张桌子: Table 1. users 'first_name', 'last_name', 'bio', 'image', 'email', 'password', 'user_group','remember_token', Table 2. professions 'id', 'title', Table 3 user_profesions “id”、“用户id”、“职业id” 我将用户表与用户表组合如下: public function index(){

我有三张桌子:

Table 1. users

'first_name', 'last_name', 'bio', 'image', 'email', 'password', 'user_group','remember_token',

Table 2. professions

'id', 'title',

Table 3 user_profesions
“id”、“用户id”、“职业id”

我将用户表与用户表组合如下:

    public function index(){


        $mentors = User::where('user_group', 2)->get();



    $all_professions = Profession::get();

        foreach($mentors as $mentor) {
            $mentor->professions =  UserProfession::where('user_id', $mentor->id)->get();
        } 



dd($mentors);
        return view('mentors.list-of-mentors')->with(['mentors'=>$mentors, 'all_professions' => $all_professions]); 
    }
所以当我死的时候它给了我这个

我还通过了专业表中的所有专业。 看来我是这样做的

  @foreach($mentors as $mentor)
{{$mentor->first_name}}
    @foreach($all_professions as $profession)
                {{$profession}}
                 @endforeach

@endforeach

我被困在这里,我不知道如何让特定用户的职业出现。。有人能帮我吗?

将关系方法添加到您的模型中。 用户模型

public function professions()
{
    return $this->hasMany(Profession::class);
}
专业模式:

public function users()
{
    return $this->hasMany(User::class);
}
现在你可以这样做了:

$user = User::where('user_group', 2)->with('professions')->get();
dd($user->professions);

将关系方法添加到模型中。 用户模型

public function professions()
{
    return $this->hasMany(Profession::class);
}
专业模式:

public function users()
{
    return $this->hasMany(User::class);
}
现在你可以这样做了:

$user = User::where('user_group', 2)->with('professions')->get();
dd($user->professions);

您可以使用雄辩的多人关系来获取
用户
专业
模型之间的数据。为此,你必须这样定义它

  @foreach($mentors as $mentor)
{{$mentor->first_name}}
    @foreach($all_professions as $profession)
                {{$profession}}
                 @endforeach

@endforeach
用户模型

public function professions()
{
    return $this->belongsToMany('App\Profession','user_profesions');
}
职业模式

public function users()
{
    return $this->belongsToMany('App\User','user_profesions');
}
现在在控制器中像这样取出它

$mentors = User::with('professions')->where('user_group', 2)->get();
return view('mentors.list-of-mentors')->with('mentors',$mentors); 
看来你可以这样使用它

@foreach($mentors as $mentor)
      {{$mentor->first_name}}
      @foreach($mentor->professions as $profession)
           {{$profession}}
      @endforeach
@endforeach

选中此处

您可以使用雄辩的多人关系来获取
用户
专业
模型之间的数据。为此,你必须这样定义它

  @foreach($mentors as $mentor)
{{$mentor->first_name}}
    @foreach($all_professions as $profession)
                {{$profession}}
                 @endforeach

@endforeach
用户模型

public function professions()
{
    return $this->belongsToMany('App\Profession','user_profesions');
}
职业模式

public function users()
{
    return $this->belongsToMany('App\User','user_profesions');
}
现在在控制器中像这样取出它

$mentors = User::with('professions')->where('user_group', 2)->get();
return view('mentors.list-of-mentors')->with('mentors',$mentors); 
看来你可以这样使用它

@foreach($mentors as $mentor)
      {{$mentor->first_name}}
      @foreach($mentor->professions as $profession)
           {{$profession}}
      @endforeach
@endforeach

检查这里

它给我这个foreach的错误($mentor->professions as$profession)它说语法错误,意外的'foreach'(T_foreach)你添加了关系吗?是的;在死后,你放弃了你需要删除的,
dd
和php foreach只是一个例子让我更新它给我这个foreach的错误($mentor->professions as$profession它说语法错误,意外的'foreach'(T_foreach)您添加了关系吗?是的;在die和dump yu放弃后,您需要删除,
dd
,php foreach只是一个示例,让我更新一下