Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

verryp's avatar

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'order clause'

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'order clause' (SQL: select * from branches order by created_at desc limit 5 offset 0)"

0 likes
8 replies
Cronix's avatar

The error seems pretty self explanatory. You don't have a created_at field in the branches table. If you're not using created_at or updated_at timestamp fields on that model, you can disable them with public $timestamps = false; in your model properties.

https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions (scroll down a bit to "Timestamps")

1 like
shez1983's avatar

also if you are using order() or latest() in your query they may use created_at by default so you have to specify the column you want to order by..

Cronix's avatar
$result = SomeModel::latest('your_datetime_column')->get();

But, it's really just a shortcut for

$result = SomeModel::orderBy('your_datetime_column', 'desc')->get();
Grelav's avatar

In Your Modal add the following for your relation withTimestamps();

example

class Article extends Model
{

//  ........


public function categories()
{
      return $this->belongsToMany('\App\Category')->withTimestamps();
}

// .......


}
Snapey's avatar

Do you have timestamps on your migration for this model?

Snapey's avatar

so if you want to sort the records by date, you need a datetime column

What query are you running to produce the SQL in your original question?

Please or to participate in this conversation.