【问题标题】:Laravel Unknown Column 'updated_at'Laravel 未知列 'updated_at'
【发布时间】:2015-04-01 09:52:58
【问题描述】:

我刚开始使用 Laravel,我收到以下错误:

未知列“updated_at”插入到 gebruikers(naam、wachtwoord、 updated_at, created_at)

我知道错误来自迁移表时的时间戳列,但我没有使用updated_at 字段。当我遵循 Laravel 教程时,我曾经使用它,但现在我正在制作(或尝试制作)我自己的东西。即使我不使用时间戳,我也会收到此错误。我似乎找不到使用它的地方。这是代码:

控制器

public function created()
{
    if (!User::isValidRegister(Input::all())) {
        return Redirect::back()->withInput()->withErrors(User::$errors);
    }

    // Register the new user or whatever.
    $user = new User;
    $user->naam = Input::get('naam');
    $user->wachtwoord = Hash::make(Input::get('password'));
    $user->save();

    return Redirect::to('/users');
}

路线

Route::get('created', 'UserController@created');

型号

public static $rules_register = [
    'naam' => 'unique:gebruikers,naam'
];

public static $errors;
protected $table = 'gebruikers';

public static function isValidRegister($data)
{
    $validation = Validator::make($data, static::$rules_register);

    if ($validation->passes()) {
        return true;
    }

    static::$errors = $validation->messages();

    return false;
}

我一定是忘记了什么……我在这里做错了什么?

【问题讨论】:

  • 如果您有列 updated_at,请检查您的表!
  • @MehdiMaghrooni 我不知道。
  • 这就是问题所在,您想访问甚至不存在的列。你要么改变你的表来添加一个,要么干脆删除那个。
  • @bad_boy 我什至没有在我的代码中的任何地方使用 updated_at。
  • @bad_boy 我只需要在模型中将时间戳设置为 false...

标签: php mysql laravel laravel-migrations


【解决方案1】:

如果您仍然需要时间戳,但只是忘记在迁移中添加它们,将以下内容添加到迁移文件中也可以:

class AddUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->timestamps(); // <-- Add this to add created_at and updated_at
        });
    }
}

不要忘记之后重新运行迁移。

php artisan migrate:rollback
php artisan migrate

【讨论】:

    【解决方案2】:

    将时间戳设置为 false 意味着您将丢失 created_at 和 updated_at 而您可以在模型中设置这两个键。

    案例 1:

    您有 created_at 列但没有 update_at 您可以简单地将模型中的 updated_at 设置为 false

    class ABC extends Model {
    
    const UPDATED_AT = null;
    

    案例 2:

    您同时拥有 created_atupdated_at 列,但列名不同

    你可以这样做:

    class ABC extends Model {
    
    const CREATED_AT = 'name_of_created_at_column';
    const UPDATED_AT = 'name_of_updated_at_column';
    

    最后完全忽略时间戳:

    class ABC extends Model {
    
    public $timestamps = false;
    

    【讨论】:

    • 这应该是正确的答案。设置时间戳会删除这两个字段。谢谢!!!
    • 这是正确的答案,因为问题中显示的错误中的刚刚 updated_at 字段。
    • 这个答案是最好的
    • 当尝试将 UPDATED_AT 常量设置为 false 时,我得到“array_key_exists():第一个参数应该是字符串或整数”并且在任何地方都找不到关于如何解决该问题的答案。最后不得不使用 $timestamps 解决方案并依赖手动设置值。
    • 工作解决方案
    【解决方案3】:

    在模型中,编写如下代码;

    public $timestamps = false;
    

    这会起作用。

    说明:默认情况下,laravel 会在您的表中期望 created_at 和 updated_at 列。 通过将其设置为 false,它将覆盖默认设置。

    【讨论】:

    • @RameshPareek 在docs中有说明:By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false
    • 除非这是出于审计目的,否则我不明白为什么一开始就需要这样做,应该默认关闭并作为选项启用。
    • 有趣的是,在迁移中您必须指定创建时间戳,如下所示:$table-&gt;timestamps(); 所以在迁移中时间戳默认为关闭,但在模型中默认为开启。
    • 这很有帮助,谢谢
    【解决方案4】:

    Alex 和 Sameer 的回答很好,但可能只是关于为什么需要放置的附加信息

    public $timestamps = false;
    

    官方Laravel page上很好地解释了时间戳:

    默认情况下,Eloquent 期望 created_at 和 updated_at 列存在于您的 >table 中。如果您不希望 >Eloquent 自动管理这些列,请将模型上的 $timestamps 属性设置为 false。

    【讨论】:

      【解决方案5】:

      使用 laravel 5 或以上版本的用户必须使用 public 修饰符,否则会抛出异常

      Access level to App\yourModelName::$timestamps must be
      public (as in class Illuminate\Database\Eloquent\Model)
      
      public $timestamps = false;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-13
        • 2018-04-05
        • 2017-11-05
        • 2018-03-23
        • 1970-01-01
        • 2015-02-22
        • 2020-01-27
        • 1970-01-01
        相关资源
        最近更新 更多