238

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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Loko
  • 6,539
  • 14
  • 50
  • 78

6 Answers6

641

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.

Sameer Shaikh
  • 7,564
  • 2
  • 17
  • 32
  • 10
    @RameshPareek It is stated in the [docs](https://laravel.com/docs/5.6/eloquent#defining-models): `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 Jun 01 '18 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. – OzzyTheGiant Jun 08 '20 at 17:02
  • 4
    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. – Aleksey Stetsenko Sep 02 '21 at 11:19
66

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

usrNotFound
  • 2,680
  • 3
  • 25
  • 41
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;
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50
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
TheKeymaster
  • 857
  • 1
  • 11
  • 27
1

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
bluestar0505
  • 338
  • 1
  • 14