【问题标题】:laravel Column not found: 1054 Unknown column 'created_at' in 'order clause'laravel Column not found: 1054 Unknown column 'created_at' in 'order Clause'
【发布时间】:2018-09-10 16:57:43
【问题描述】:

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

例如当我访问页面时 - http://mypage/api/videos/21/comments 我收到一个错误 - “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 函数

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

【问题讨论】:

  • index函数的查询中删除-&gt;latest()
  • Hello @LightScribe VideoCommentController.php 文件包括用于查询的 latest() 删除。最新和最旧的方法允许您轻松按日期排序结果。默认情况下,结果将按 created_at 列排序。
  • @Sohel0415 Mayur Panchal。谢谢这对我有帮助。

标签: php mysql laravel


【解决方案1】:

当您没有created_at 列时,您可以轻松地使用orderByfirst() 而不是latest()

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-31
        • 2020-10-13
        • 2011-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        • 1970-01-01
        相关资源
        最近更新 更多