英文:
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
它应该显示与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,
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('specialities')->where('id', 5)->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('users')->where('id', 5)->get();
> Please note that the above is an array
Example of a Controller Function:
public function results($id){
$results = Speciality::with('users')->where('id', $id)->get();
return view('user_specialities', compact('results'));
}
Example view user_specialities:
@foreach($results as $result)
<p>Result: {{$result->result}}</p>
<p>Formulary: {{$result->formulary}}</p>
<p>Color: {{$result->color}}</p>
<p>User ID: {{$result->user->id}}</p>
<p>User Name: {{$result->user->name}}</p>
@endforeach
Speciality Model:
public function users()
{
return $this->belongsToMany(User::class, 'results', 'specialitie_id', 'user_id')->withPivot('result', 'color');
}
User Model:
public function specialities()
{
return $this->belongsToMany(Speciality::class, 'results', 'user_id', 'specialitie_id')->withPivot('result', 'color');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论