laravel 页面未找到:1054“order子句”中存在未知列“created_at”

qoefvg9y  于 2023-01-27  发布在  其他
关注(0)|答案(3)|浏览(152)

我已经定义了不使用时间戳,但是laravel仍然强制使用时间戳...我使用的是laravel 5.6。
当我访问页面时,例如-http://mypage/api/videos/21/comments,我收到一个错误-"SQLSTATE [42S22]:未找到列:1054 "order子句"中的未知列"created_at"(SQL:从videos_comments中选择 *,其中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');
}
}

视频评论控制器

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

ax6ht2ek1#

最新和最旧的方法允许您轻松地按日期对结果进行排序。默认情况下,结果将按created_at列排序。

v2g6jxz6

v2g6jxz62#

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

55ooxyrt

55ooxyrt3#

当您没有created_at列时,可以轻松地使用orderByfirst()来代替latest()

相关问题