laravel collections add code example

Example 1: collection map laravel

// The array we're going to return
    $data = [];    
// Query the users table
$query = users::where('id', 1)->get();

// Let's Map the results from [$query]
$map = $query->map(
    function($items){
          $data['user_firstName'] = $items->firstName;
          $data['user_lastName'] = $items->lastName;
          return $data;
        }
    );

return $map;

Example 2: push collection php

$collection = collect([1, 2, 3, 4]);

$collection->push(5);

$collection->all();

// [1, 2, 3, 4, 5]

Tags:

Php Example