ChatGPT解决这个技术问题 Extra ChatGPT

Laravel Unknown Column 'updated_at'

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

Unknown column 'updated_at' insert into 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_at field. 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:

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?

check your table if you have column updated_at !
@MehdiMaghrooni I dont.
And that's the problem, you want to access the column which doesn't even exists. You either gotta alter your table to add the one, or simply remove that one.
@bad_boy Im not even using updated_at anywhere in my code.
@bad_boy I just had to put the timestamps on false in the model...

S
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.


@RameshPareek It is stated in the 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
Unless this is for audit purposes, I'm not seeing why this is necessary to begin with, should probably be turned off by default and enabled as an option.
Interesting that in migrations you have to specify creating timestamps, like so: $table->timestamps(); So in migrations timestamp is off by default, but in models is on by default.
this was helpful, thank you
u
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.

Case 1:

You have created_at column but not update_at you could simply set updated_at to false in your model

class ABC extends Model {

const UPDATED_AT = null;

Case 2:

You have both created_at and updated_at columns but with different column names

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

Link to laravel documentation https://laravel.com/docs/9.x/eloquent#timestamps


this should be the correct answer. Setting timestamps removes both of the fields. Thanks!!!
This is the correct answer due to the just updated_at field in the error showed in the question.
This answer is the best one
When tried setting the UPDATED_AT constant to false, I got "array_key_exists(): The first argument should be either a string or an integer" and couldn't find an answer anywhere on how to solve that. Had to use the $timestamps solution in the end and rely on manually setting the values.
working solution
佚名

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:

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.


B
Basheer Kharoti

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

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

public $timestamps = false;

T
TheKeymaster

In case you still want the timestamps, but simply forgot to add them in the migration, adding the following to your migration file, will also work:

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

Don't forget to re-run your migration afterwards.

php artisan migrate:rollback
php artisan migrate

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now