Skip to content

Instantly share code, notes, and snippets.

@marcelod
Forked from IgorDePaula/collection.php
Created November 25, 2020 18:33
Show Gist options
  • Save marcelod/429f5b51fee9ac4bd9a80b65a174ae1a to your computer and use it in GitHub Desktop.
Save marcelod/429f5b51fee9ac4bd9a80b65a174ae1a to your computer and use it in GitHub Desktop.

Revisions

  1. @IgorDePaula IgorDePaula created this gist Nov 25, 2020.
    20 changes: 20 additions & 0 deletions collection.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    $items = [];
    foreach ($products as $value) {
    $items[] = [
    'product_id' => $value->id,
    'name' => $value->name,
    ];
    }
    return collect($items)->all();
    ==================================

    return collect($products)
    ->map(function($product){
    return ['product_id' => $product->id,'name' => $product->name];
    })
    ->toArray();

    =================================
    return Product::all()->pluck('id','name');