与参数的关系然后使用 contains in Eloquent Laravel
Relationship with parameter then use contains in Eloquent Laravel
让下一段代码正常工作:
$system_breeds = SystemsGroup::find($group_id)->breeds;
$breeds = Breed::select('id', 'breed')->orderBy('breed')->get()->transform(function ($breed) use ($system_breeds) {
if ($system_breeds->contains($breed)) {
$breed->active = true;
}
return $breed;
});
在 system_breeds 上存在的每个品种上,我添加一个活跃的 属性 为真。
我现在想做的,是这样的:
$system_breeds = SystemsGroup::where('id',$group_id)->with(['breeds' => function($q) use ($section_id){
$q->where('group_section_id', $section_id);
}])->get();
因此,在关系上添加一个 where 过滤器。但是 "contains" 不起作用。
我猜 $system_breeds 是一个数组。尝试使用
将其转换为集合
collect($system_breeds)
一旦它成为 Collection class 的一个实例,'contains' 方法应该可以工作。
你的问题不是很清楚,看你是想过滤并获取相关模型,如果是这样试试。
$system_breeds = SystemsGroup::find($group_id)->breeds()->where('group_section_id',$section_id)->get();
让下一段代码正常工作:
$system_breeds = SystemsGroup::find($group_id)->breeds;
$breeds = Breed::select('id', 'breed')->orderBy('breed')->get()->transform(function ($breed) use ($system_breeds) {
if ($system_breeds->contains($breed)) {
$breed->active = true;
}
return $breed;
});
在 system_breeds 上存在的每个品种上,我添加一个活跃的 属性 为真。
我现在想做的,是这样的:
$system_breeds = SystemsGroup::where('id',$group_id)->with(['breeds' => function($q) use ($section_id){
$q->where('group_section_id', $section_id);
}])->get();
因此,在关系上添加一个 where 过滤器。但是 "contains" 不起作用。
我猜 $system_breeds 是一个数组。尝试使用
collect($system_breeds)
一旦它成为 Collection class 的一个实例,'contains' 方法应该可以工作。
你的问题不是很清楚,看你是想过滤并获取相关模型,如果是这样试试。
$system_breeds = SystemsGroup::find($group_id)->breeds()->where('group_section_id',$section_id)->get();