Laravel 使用多对多关系获取中间表数据

huangapple go评论75阅读模式
英文:

Laravel fetch intermediate table data with many to many relationship

问题

我有3张表
user, specialities和results

results表包含specialitie_id和user_id

我已经创建了多对多关系

以下是user模型中的关系

public function specialities()
{
    return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
}

我想要根据specialitie_id从结果表中获取数据

如果我的URL是
abc.com/result/5

我也附上了表结构
Laravel 使用多对多关系获取中间表数据

它应该显示与specialitie_id 5相关的结果
我尝试过这个但不起作用

$result = User::with('specialities')->where('id', 5)->get();

任何帮助都将不胜感激。

英文:

I have 3 tables
user, specialities & results

results contain specialitie_id, user_id

I have create many-to-many relationship

Here's the relationship in user model

public function specialities()
    {
        return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
    }

I want to fetch data from the result table based on specialitie_id

If my url is
abc.com/result/5

I have attached table structure as well,
Laravel 使用多对多关系获取中间表数据

it should show me result related to specialitie_id 5
I tried this but it doesn't work

$result = User::with('specialities')->where('id', 5)->get();

Any help would be appreciated.

答案1

得分: 2

$result = User::with('specialities')->where('id', 5)->get();

说,给我所有的结果,其中 用户ID = 5,与之相关的 所有特长。


你很接近,但是把它搞反了:

// 给我所有 SPECIALITY_ID = 5 的结果
// 与其相关的所有用户
$result = Speciality::with('users')->where('id', 5)->get(); 

请注意上面的结果是一个数组

控制器函数示例:

public function results($id){
    $results = Speciality::with('users')->where('id', $id)->get(); 

    return view('user_specialities', compact('results'));
}

示例视图 user_specialities

@foreach($results as $result)
    <p>结果:{{$result->result}}</p>
    <p>表单:{{$result->formulary}}</p>
    <p>颜色:{{$result->color}}</p>
    <p>用户ID:{{$result->user->id}}</p>
    <p>用户名:{{$result->user->name}}</p>
@endforeach

Speciality 模型:

public function users()
{
    return $this->belongsToMany(User::class, 'results', 'specialitie_id', 'user_id')->withPivot('result', 'color');
}

User 模型:

public function specialities()
{
    return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
}
英文:

Your current query:

$result = User::with(&#39;specialities&#39;)-&gt;where(&#39;id&#39;, 5)-&gt;get();

> Is saying, give me all results where the USER_ID = 5 WITH all the specialities related to it.


You are very close but did it backwards:

//Give me all results where the SPECIALITY_ID = 5 
//WITH all the users related to it
$result = Speciality::with(&#39;users&#39;)-&gt;where(&#39;id&#39;, 5)-&gt;get(); 

> Please note that the above is an array

Example of a Controller Function:

public function results($id){
	$results = Speciality::with(&#39;users&#39;)-&gt;where(&#39;id&#39;, $id)-&gt;get(); 

	return view(&#39;user_specialities&#39;, compact(&#39;results&#39;));
}

Example view user_specialities:

@foreach($results as $result)
	&lt;p&gt;Result: {{$result-&gt;result}}&lt;/p&gt;
	&lt;p&gt;Formulary: {{$result-&gt;formulary}}&lt;/p&gt;
	&lt;p&gt;Color: {{$result-&gt;color}}&lt;/p&gt;
	&lt;p&gt;User ID: {{$result-&gt;user-&gt;id}}&lt;/p&gt;
	&lt;p&gt;User Name: {{$result-&gt;user-&gt;name}}&lt;/p&gt;
@endforeach

Speciality Model:

public function users()
{
    return $this-&gt;belongsToMany(User::class, &#39;results&#39;, &#39;specialitie_id&#39;, &#39;user_id&#39;)-&gt;withPivot(&#39;result&#39;, &#39;color&#39;);
}

User Model:

public function specialities()
    {
        return $this-&gt;belongsToMany(Speciality::class, &#39;results&#39;, &#39;user_id&#39;, &#39;specialitie_id&#39;)-&gt;withPivot(&#39;result&#39;, &#39;color&#39;);
    }

huangapple
  • 本文由 发表于 2023年1月9日 18:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定