laravel collection foreach code example

Example 1: laravel loop through collection

$users = App\User::where('active', 1)->get();

foreach ($users as $user) {
    echo $user->name;
}

Example 2: foreach in laravel

foreach ($users as $user) {
    // stuff here
}

Example 3: 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;