Answers for "how to get item from collection in laravel by key"

PHP
1

laravel collection find by key

$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

Code answers related to "how to get item from collection in laravel by key"

Browse Popular Code Answers by Language