Meda
Meda

Reputation: 65

how to insert data into database in laravel

I am trying to insert data into one table, my table have three columns, id-auto increment, phoneno, login_date. i am trying the below code.

$input = Request::has('phoneno');
$login_history = new LoginHistory;
$login_history->phoneno=$input;
$login_history->login_date=date('Y-m-d H:i:s');
$login_history->save();

but i am getting the below error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into login_history (phoneno, login_date, updated_at, created_at) values (1, 2015-12-14 08:55:25, 2015-12-14 08:55:25, 2015-12-14 08:55:25))

Upvotes: 2

Views: 1185

Answers (3)

Vishal Sharma
Vishal Sharma

Reputation: 2610

The reason for this error is simple. Laravel by default assumes that your table has created_at and updated_at fields. hence, ->save() method tries to store the timestamps as you can see in the query.

If you do not want these to be included, add public $timestamps = false; in your Model.

Upvotes: 2

Christian Giupponi
Christian Giupponi

Reputation: 7618

This is because you do not have the field updated_at in your database table.
Make sure to add:

$table->timestamps()

to your migration and re-migrate the database ( or update the table ).

This will add the fields created_at and updated_at.

Upvotes: 2

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

You don't have created_at and updated_at columns in your table that Eloquent expects all models have by default. Either create those columns or simply disable timestamps for that model by setting $timestamps property accordingly:

class LoginHistory {
  protected $timestamps = false;
}

Upvotes: 2

Related Questions