Laravel Unknown Column 'updated_at'

6 浏览
0 Comments

Laravel Unknown Column 'updated_at'

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

Unknown column \'updated_at\' insert into 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;
}

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

admin 更改状态以发布 2023年5月24日
0
0 Comments

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

情况1:

您有一个created_at列,但没有updated_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;
}

链接到laravel文档 https://laravel.com/docs/9.x/eloquent#timestamps

0
0 Comments

在模型中,写下面的代码;

public $timestamps = false;

这将起作用。

解释:默认情况下,laravel将期望在您的表中有created_at和updated_at列。将其设置为false,它将覆盖默认设置。

0