LightScribe
LightScribe

Reputation: 41

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

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());
   }

Upvotes: 4

Views: 11330

Answers (3)

Mayur Panchal
Mayur Panchal

Reputation: 655

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.

Upvotes: 3

Pooya Sabramooz
Pooya Sabramooz

Reputation: 342

You can easily use orderBy and first() instead of latest() when you haven't created_at column.

Upvotes: 2

MD Iyasin Arafat
MD Iyasin Arafat

Reputation: 763

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

Upvotes: 3

Related Questions