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

csaba_szekely's avatar

Get user array with id as key and user name as value

Hi!

How do I get a list of array of the users table where the key is the user_id and the value is the user's name, for the users where the id is > 10?

0 likes
1 reply
LaryAI's avatar
Level 58

You can achieve this using Laravel's pluck method. Here's an example:

$userArray = User::where('id', '>', 10)->pluck('name', 'id')->toArray();

This will give you an array where the keys are the user IDs and the values are the user names. The pluck method selects the name and id columns from the users table and creates an array where the id column is used as the key and the name column is used as the value. The toArray method is used to convert the collection to an array.

1 like

Please or to participate in this conversation.