Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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;
    }
}
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ventaquil ventaquil Joined 30 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.