laravel collection to object code example
Example 1: laravel convert eloquent collection to collection
collect(Order::get()->all());
Example 2: get item from collection that match
$artist = 'Vincent John Doe';
// you can filter the collection and keep only those items
// that pass a given truth test:
$art_collection = $paintings->filter(function ($painting) use ($artist) {
return $painting->artist == $artist;
});
// you can also do a foreach
foreach($paintings as $painting) {
if($painting->artist == $artist) {
$art_collection[] = $painting;
}
}
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;