keyBy()
accepts a closure for defining keys for every array item. You could do something like this:
use Carbon\Carbon;
$array = $resultsCollection->keyBy(function($item) {
return Carbon::parse($item->time)->timestamp;
});
Or, if you still want that utime
attribute for something else, not just for keyBy
, you can add it to your model like this:
class MyModel extends Eloquent {
protected $appends = array('utime');
public function getUtimeAttribute()
{
return \Carbon\Carbon::parse($this->time)->timestamp;
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community