[英]Pluck the values from WITH Eloquent in Laravel
我有一個下拉框,只顯示具有收藏家和借款人角色的用戶。 這是我的代碼
public function create()
{
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');
})
->with('profile')
->get()
->pluck('name','id');
return view('dashboard.documents.create', compact('users'));
}
這段代碼很好,但我要選擇的是“個人資料”中的列 first_name、last_name 和 user_id。 我怎樣才能做到這一點?
非常感謝您!
你做其中之一
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->select('name','id')
->get();
或者
$users = User::whereHas('roles', function ($q) {
$q->where('roles.name', '=', 'collector')->orWhere('roles.name', '=', 'borrower');})
->with('profile')
->get(['name','id']);
這兩種方式都將返回一個帶有相關模型的collection
,每個模型記錄將具有選定的列,如果要將其轉換為數組,您可以在兩種方式之后添加->toArray()
,它將集合轉換為數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.