laravel map code example

Example 1: {{Str::limit laravel

{{Str::limit($category->name, 20)}}

if to end 
  
{{Str::limit($category->name, 20, $end='.......')}}
             
or
  
{{\Illuminate\Support\Str::limit($category->name, 20)}}

Example 2: laravel str::random

use Illuminate\Support\Str;

$random = Str::random(40);

Example 3: 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 4: laravel collection methods

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

$collection->each(function($item){
    return $item*$item;
});

// [1,4,9,16]

Example 5: eloquent map collection

// 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;

Tags:

Php Example