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());
}
->latest()
from query inindex
function – Sohel0415