Answers for "laravel collection by keys"

PHP
1

laravel collection keyBy

$collection = collect([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);

$keyed = $collection->keyBy('product_id');

$keyed->all();

/*
    [
        'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
        'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]
*/
Posted by: Guest on May-02-2021
0

laravel collection modelKeys

$users->modelKeys();

// [1, 2, 3, 4, 5]
Posted by: Guest on July-24-2021

Code answers related to "laravel collection by keys"

Browse Popular Code Answers by Language