3
votes

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());
   }
3
remove ->latest() from query in index functionSohel0415
Hello @LightScribe VideoCommentController.php file include latest() remove for the query.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.Mayur Panchal
@Sohel0415 Mayur Panchal. Thanks that helped for me.LightScribe

3 Answers

6
votes

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.

2
votes

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

1
votes

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