php Laravel 未知列'updated_at'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28277955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:46:57  来源:igfitidea点击:

Laravel Unknown Column 'updated_at'

phpmysqllaravellaravel-migrations

提问by Loko

I've just started with Laravel and I get the following error:

我刚开始使用 Laravel,但出现以下错误:

Unknown column 'updated_at' insert into gebruikers (naam, wachtwoord, updated_at, created_at)

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

I know the error is from the timestamp column when you migrate a table but I'm not using the updated_atfield. I used to use it when I followed the Laravel tutorial but now that I am making (or attempting to make) my own stuff. I get this error even though I don't use timestamps. I can't seem to find the place where it's being used. This is the code:

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

Controller

控制器

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

路线

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

Model

模型

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;
}

I must be forgetting something... What am I doing wrong here?

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

回答by Sameer Shaikh

In the model, write the below code;

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

public $timestamps = false;

This would work.

这会奏效。

Explanation : By default laravel will expect created_at & updated_at column in your table. By making it to false it will override the default setting.

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

回答by usrNotFound

Setting timestamps to false means you are going to lose both created_at and updated_at whereas you could set both of the keys in your model.

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

Case 1:

情况1:

You have created_atcolumn but not update_at you could simply set updated_atto false in your model

你有created_atcolumn 但没有 update_at 你可以简单地updated_at在你的模型中设置为 false

class ABC extends Model {

const UPDATED_AT = null;

Case 2:

案例2:

You have both created_atand updated_atcolumns but with different column names

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

You could simply do:

你可以简单地做:

class ABC extends Model {

const CREATED_AT = 'name_of_created_at_column';
const UPDATED_AT = 'name_of_updated_at_column';

Finally ignoring timestamps completely:

最后完全忽略时间戳:

class ABC extends Model {

public $timestamps = false;

回答by usrNotFound

Nice answer by Alex and Sameer, but maybe just additional info on why is necessary to put

亚历克斯和萨米尔的回答很好,但也许只是关于为什么需要放置的附加信息

public $timestamps = false;

Timestamps are nicely explained on official Laravel page:

时间戳在官方Laravel 页面上有很好的解释:

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.

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

回答by Clean code

For those who are using laravel 5 or above must use public modifier other wise it will throw an exception

对于那些使用 laravel 5 或更高版本的人,必须使用 public 修饰符,否则会抛出异常

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

public $timestamps = false;