Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adhik13th's avatar

How to Select Dropdown using pluck using Foreign Key

I'd like to show names on my select dropdown

I have 3 tables: 'users','users_pro' and 'practice'

users : id ,name ,dob ,.. etc

users_pro : id ,user_id,..etc

practice : id , user_pro_id ,...etc

I want to input data to table 'practice' and I want to make a select dropdown using pluck on users_pro with users like this :

public function input_practice()
{
    $data= Users_pro::with('users')->get()->pluck('id','user_id');
    
    dd($data);
    // return view('admin.input.input_riwayat_kgb',
    // [
    // 'data'=>$data,
    

    // ]);
}

If I dd($data), I get :

    Collection {#327 ▼
  #items: array:4 [▼
    6 => 3 // 6 is user_id ,and 3 is 'id' on users_pro , i need to show 'name' like 3 => 'jhon'
    13 => 4
    12 => 5
    14 => 6
  ]
}

But I want to show the name of the user on my select dropdown. Can someone help me?

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

In your Users_pro model you should have this relationship:

public function user()
{
    return $this->belongsTo(User::class);
}

Then you can use the following:

$data= Users_pro::with('user')->get()->pluck('user.name', 'id');

In pluck the second parameter is the key.

Please or to participate in this conversation.