Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel Column not found: 1054 Unknown column 'created_at' in 'order clause'

Tags:

php

mysql

laravel

I have defined to not use timestamps, but still laravel forces to use timestaps... Im using laravel 5.6.

When I visit page for example - http://mypage/api/videos/21/comments I get an error - "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'order clause' (SQL: select * from videos_comments where videos_comments.video_id = ▶"

app/Providers/VideoComment.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class VideoComment extends Model
{
protected $table = 'videos_comments';
public $timestamps = false;
protected $fillable = [
  'text', 'userid', 'date'
];
public function videos() {
  return $this->belongsTo('App\Video', 'id', 'video_id');
}
public function member() {
  return $this->belongsTo('App\Member', 'userid', 'member_id');
}
}

app/Providers/Video.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class Video extends Model
{
protected $table = 'videos';
public $timestamps = false;
use Sluggable;

public function sluggable() {
  return [
      'slug' => [
          'source' => 'title'
      ]
    ];
}
public function comments() {
  return $this->hasMany('App\VideoComment', 'video_id', 'id');
}
public function member() {
  return $this->belongsTo('App\Member', 'userid', 'member_id');
}
}

VideoCommentController.php function

   public function index(Video $video) {
    return response()->json($video->comments()->with('member')->latest()->get());
   }
like image 706
LightScribe Avatar asked Sep 18 '25 07:09

LightScribe


2 Answers

If you use latest() in your query then you have to add created_at in your db table, read more.

like image 121
MD Iyasin Arafat Avatar answered Sep 19 '25 21:09

MD Iyasin Arafat


The latest and oldest methods allow you to easily order results by date. By default, the result will be ordered by the created_at column.

like image 30
Mayur Panchal Avatar answered Sep 19 '25 21:09

Mayur Panchal