gkileo
gkileo

Reputation: 21

Is it possible to generate associative array from laravel pluck()

ModalName::pluck('id')->toArray();

i want id to be associative array with certain defined key. such as 'my_key'=>id in a pluck

Upvotes: 0

Views: 956

Answers (1)

Qonvex620
Qonvex620

Reputation: 3972

First of all pluck() return you already an array so no need to call toArray().

Yes you can make it associative by passing another argument on pluck method. Like this

$plucked = $collection->pluck('name', 'product_id');

sample result

['prod-100' => 'Desk', 'prod-200' => 'Chair']

please see docs here source

so in your case

ModalName::all()->pluck('id', 'name'); // name = field in your table

Upvotes: 1

Related Questions