[英]Laravel Pluck from 'with relation'
需要與關系有聯系。 我在下面分享代碼。
我的 controller:
$type = StandType::with(['brieftype' => function($q) use ($brief_id){
$q->where('brief_id', $brief_id);
}])->get()->keyBy('name');
我的Model:
public function brieftype(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany('App\Models\BriefType', 'key', 'name');
}
我想這樣回應:
[Blabla] => {
'title' => 'my title',
'name' => 'my name'
'brieftype' => ['name1', 'name2', 'name3', 'name4']
}
試試這個。
$types = $types->map(function ($type) {
$type->brieftype = $type->brieftype->pluck('name')->toArray();
return $type;
})->toArray();
做這個。 它將返回一個帶有 brieftype 的集合
$types = $types->map(function ($type) {
$type->brieftype = $type->brieftype->pluck('name');
return $type;
});
dd($types);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.