collect laravel code example
Example 1: laravel sort collection
$collection = collect([5, 3, 1, 2, 4]);
$sorted = $collection->sortDesc();
$sorted->values()->all();
Example 2: collection map laravel
$data = [];
$query = users::where('id', 1)->get();
$map = $query->map(
function($items){
$data['user_firstName'] = $items->firstName;
$data['user_lastName'] = $items->lastName;
return $data;
}
);
return $map;
Example 3: laravel collection methods
$collection = collect([1,2,3,4]);
$collection->each(function($item){
return $item*$item;
});
Example 4: collection methods laravel
$collection->each(function ($item, $key) {
hj
});