简体   繁体   中英

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))

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM