未找到laravel列:1054'order clause'

时间:2018-04-01 04:58:27

标签: php mysql laravel

我已经定义了不使用时间戳,但仍然使用时间戳的laravel强制...我正在使用laravel 5.6。

当我访问页面时 - http://mypage/api/videos/21/comments 我收到错误 - “SQLSTATE [42S22]:未找到列:1054未知列'created_at'在'order子句'中(SQL:select * from videos_comments where videos_commentsvideo_id = ▶“

应用程序/提供者/ 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');
}
}

应用程序/提供者/ 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功能

   public function index(Video $video) {
    return response()->json($video->comments()->with('member')->latest()->get());
   }

3 个答案:

答案 0 :(得分:5)

最新和最早的方法可让您轻松按日期订购结果。默认情况下,结果将由created_at列排序。

答案 1 :(得分:1)

如果在查询中使用latest(),则必须在db表中添加created_at,read more.

答案 2 :(得分:0)

如果您没有orderBy列,则可以轻松使用first()latest()代替created_at