247

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?

5
  • check your table if you have column updated_at ! Commented Feb 2, 2015 at 12:40
  • @MehdiMaghrooni I dont.
    – Loko
    Commented Feb 2, 2015 at 12:41
  • 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.
    – Yang
    Commented Feb 2, 2015 at 12:42
  • @bad_boy Im not even using updated_at anywhere in my code.
    – Loko
    Commented Feb 2, 2015 at 12:43
  • @bad_boy I just had to put the timestamps on false in the model...
    – Loko
    Commented Feb 2, 2015 at 12:49

6 Answers 6

661

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.

3
  • 11
    @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
    – Adam
    Commented Jun 1, 2018 at 16:44
  • 1
    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. Commented Jun 8, 2020 at 17:02
  • 5
    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. Commented Sep 2, 2021 at 11:19
70

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/11.x/eloquent#timestamps

5
  • 4
    this should be the correct answer. Setting timestamps removes both of the fields. Thanks!!!
    – Sameera K
    Commented Feb 14, 2019 at 4:37
  • 3
    This is the correct answer due to the just updated_at field in the error showed in the question. Commented Mar 13, 2019 at 16:06
  • 1
    This answer is the best one
    – Mojtaba
    Commented Jul 1, 2019 at 21:06
  • 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.
    – user3168237
    Commented Dec 2, 2020 at 20:03
  • working solution Commented Nov 27, 2021 at 10:44
18

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.

14

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

First Solution

If not necessary created_at and updated_at, please write the below code in the model.

public $timestamps = false;

Second Solution

If you need to use created_at and updated_at in the future, you can add columns.

Create migration file:

php artisan make:migration add_timestamps_fields_to_users_table

Edit migration file:

class AddTimestampsFieldsToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->timestamps();
        });
    }
}

Run migration:

php artisan migrate
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.