Framework: Error if I try to group a collection by created_at or updated_at

Created on 12 Apr 2015  路  4Comments  路  Source: laravel/framework

Hi guys

I get following error:

ErrorException in Collection.php line 257:
Illegal offset type

if I execute following code:

return Pool::all()->groupBy('created_at'); // Pool is a Eloquent model.

This only happens if I try to group by a carbon date attribute.

If I execute:

return Pool::all()->groupBy('id');

or even a non existing attribute:

return Pool::all()->groupBy('foo');

then no exception is thrown.

I tried it on different models.

I'm on version 5.0.27

I also tried it on another app which sits on version 5.0.22 - same error.

Do I miss something?

Most helpful comment

That works:

return Pool::all()->groupBy(function($pool) {
    return $pool->created_at->toDateTimeString();
});

Is the error above expected behaviour and I have to group it with a closure?

All 4 comments

That works:

return Pool::all()->groupBy(function($pool) {
    return $pool->created_at->toDateTimeString();
});

Is the error above expected behaviour and I have to group it with a closure?

Yeh, you will need to use a closure.

Thanks Graham for your fast answer!

Thanks @strebl :+1:

Was this page helpful?
0 / 5 - 0 ratings