Php 拉威尔:我如何在with语句中使用where?
我有一个类似这样的url:Php 拉威尔:我如何在with语句中使用where?,php,eloquent,laravel-5.2,Php,Eloquent,Laravel 5.2,我有一个类似这样的url:site.com/atterners/1 每个运动员可以有许多位置。从上面的url中,我正在尝试获取所有属于位置id为1的运动员。我的关系很好,但我会回报所有运动员,不管他们的网址是什么 我有三张表:运动员,位置,运动员位置 athletes id name positions id name athlete_position athlete_id position_id atternate.php public
site.com/atterners/1
每个运动员可以有许多位置。从上面的url中,我正在尝试获取所有属于位置id为1的运动员。我的关系很好,但我会回报所有运动员,不管他们的网址是什么
我有三张表:运动员
,位置
,运动员位置
athletes
id
name
positions
id
name
athlete_position
athlete_id
position_id
atternate.php
public function positions()
{
return $this->belongsToMany('App\Position', 'athlete_position', 'athlete_id', 'position_id');
}
public function athletes()
{
return $this->belongsToMany('App\Athlete');
}
public function position(Position $position) {
$athletes = Athlete::with('positions')->get();
dd($athletes); // returns all athletes with the position relationship
}
Position.php
public function positions()
{
return $this->belongsToMany('App\Position', 'athlete_position', 'athlete_id', 'position_id');
}
public function athletes()
{
return $this->belongsToMany('App\Athlete');
}
public function position(Position $position) {
$athletes = Athlete::with('positions')->get();
dd($athletes); // returns all athletes with the position relationship
}
AthleteController.php
public function positions()
{
return $this->belongsToMany('App\Position', 'athlete_position', 'athlete_id', 'position_id');
}
public function athletes()
{
return $this->belongsToMany('App\Athlete');
}
public function position(Position $position) {
$athletes = Athlete::with('positions')->get();
dd($athletes); // returns all athletes with the position relationship
}
好像我需要这样的东西:
$athletes = Athlete::with('positions')->where('position_id', '=', $position->id)->get();
但这并没有正确地使用这种关系。有没有办法在模型中使用where
子句?这样,当我访问urlsite.com/atterners/2
时,我只会得到属于位置id为2的运动员
谢谢你的建议
编辑
一切都很好!非常感谢@lagbox!结果又一次证明我想得太多了
只是一个简单的后续问题。当运动员处于位置1和位置2时,最好的处理方式是什么
在我看来,当在运动员中循环时,我是否应该对照url段检查位置id以仅显示特定位置
比如:
@foreach($athletes as $athlete)
@foreach($athlete->positions as $position)
@if($position->id == {{ Request::segment(3) }}
// Show the athlete
@endif
@endforeach
@endforeach
否则,由于该运动员属于位置id 1和2,因此该运动员将在结果中出现两次。我只想让运动员出现一次——为了要求的位置
例如:site.com/运动员/position/1
这就是我现在看到的
Athlete
name: Foo
position_id: 1
Athlete
name: Foo
position_id: 2
Athlete
name: Bar
position_id: 1
Athlete
name: Bar
position_id: 2
从上面的同一个url,这是我想要得到的:
Athlete
name: Foo
position_id: 1
Athlete
name: Bar
position_id: 1
您可以使用whereHas根据条件检查是否存在关系 尽管看起来您应该能够使用您的位置模型实例,因为您已经有了反向关系设置
$athletes = $position->athletes;
// or to also have all those athletes positions
$athletes = $position->athletes()->with('positions')->get(); // egaer loading
// or if you have `athletes` loaded already
$athletes = $position->athletes->load('positions'); // with lazy eager laoding
我有一篇很有说服力的关于关系的小文章,介绍了一些查询关系的方法
更新问题:
你可能根本不需要运动员的位置,因为你知道你现有的所有运动员都有这个特殊的位置。如果是这种情况,您可以将$position
返回到视图中
return view(..., ['position' => $position]);
Athletes for Position: {{ $position->name }}
<ul>
@foreach ($position->athletes as $athlete)
<li>{{ $athlete->name }}</li>
@endforeach
</ul>
返回视图(…,['position'=>$position]);
运动员的位置:{{$Position->name}
@foreach($position->运动员作为$运动员)
- {{$atternate->name}
@endforeach
您可以使用whereHas
根据条件检查是否存在关系
尽管看起来您应该能够使用您的位置模型实例,因为您已经有了反向关系设置
$athletes = $position->athletes;
// or to also have all those athletes positions
$athletes = $position->athletes()->with('positions')->get(); // egaer loading
// or if you have `athletes` loaded already
$athletes = $position->athletes->load('positions'); // with lazy eager laoding
我有一篇很有说服力的关于关系的小文章,介绍了一些查询关系的方法
更新问题:
你可能根本不需要运动员的位置,因为你知道你现有的所有运动员都有这个特殊的位置。如果是这种情况,您可以将$position
返回到视图中
return view(..., ['position' => $position]);
Athletes for Position: {{ $position->name }}
<ul>
@foreach ($position->athletes as $athlete)
<li>{{ $athlete->name }}</li>
@endforeach
</ul>
返回视图(…,['position'=>$position]);
运动员的位置:{{$Position->name}
@foreach($position->运动员作为$运动员)
- {{$atternate->name}
@endforeach
这真是太棒了。非常感谢您的回复。你的博客文章太棒了!一切都很好-我只是有一个后续问题。我更新了我的问题。啊啊啊!我一直在用隧道式的眼光观察运动员,但我失去了一个事实,那就是我可以利用相反的角度,利用这个位置来吸引运动员。非常感谢您抽出时间!这真是太棒了。非常感谢您的回复。你的博客文章太棒了!一切都很好-我只是有一个后续问题。我更新了我的问题。啊啊啊!我一直在用隧道式的眼光观察运动员,但我失去了一个事实,那就是我可以利用相反的角度,利用这个位置来吸引运动员。非常感谢您抽出时间!