Is it possible to return an object from a laravel collection ?
$originalArr =[
'foo' =>'bar',
'foo2' => 'bar2'
]
$coll=collect($originalArr)->reject(function($item){
// do something
});
$arr = $coll->toArray();
// array
dump($arr)
// but what if I want to have an object ?
$object = (object) $arr;
dump ($object)
//So you can access the attributes like $object->foo.
But this seems fairly stupid... First make a collection , then convert to an array and then once again convert to an object.
Collections are basically an extension to the php native array. It's meant to work with arrays. The arrays can certainly contain objects, such as eloquent results, but at the core it's an array. So maybe you're using the wrong tool for the job here? It might be helpful to describe what you actually want to accomplish, rather than how you're currently trying to do it. Yes, it does look crazy doing it the way you are with your fake data, but what are you really trying to do?