Reputation: 93
I am trying to override the laravel eloquent model constants for created_at and updated_at:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Visit extends Model
{
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'last_update';
}
and then get the latest entry:
dd(Visit::latest()->get());
but am getting an unknown column error:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in
'order clause' (SQL: select * from `visits` order by `created_at` desc)"
Is there anything else that needs to be done to get this working?
Upvotes: 2
Views: 4962
Reputation: 25906
latest()
always uses created_at
by default. You have to specify the column name yourself:
Visit::latest(Visit::CREATED_AT)->get();
You can override the default behavior by adding a local scope to your Visit
model:
public function scopeLatest($query)
{
return $query->orderByDesc(static::CREATED_AT);
}
UPDATE: This will be fixed in Laravel 5.7: https://github.com/laravel/framework/pull/24004
Upvotes: 7
Reputation: 83
Cause the latest get the created_at by default check here maybe for here Same problem as you and same code
Upvotes: 0