Insert using DB table doesnt update timestamp


In laravel there are two ways to insert data/record, using DB table or using model, but i found a problem when inserting the data using DB table, the timestamp created_at and updated_at is showing '0000-00-00 00:00:00' as value.

That's the problem with DB table specially the insert method, you need to manually set the created_at and updated_at field.

Code like this will store timestamp as '0000-00-00 00:00:00':
DB::table('cars')->insert(array(
     'model_name' => 'F50', 
     'manufacture' => 'ferrari'));

Code like this will store timestamp properly:
Car::create(array(
   'model_name' => 'F50',
   'manufacture' => 'ferrari'));
or this:
$car = new Car();
$car->model_name = 'F50';
$car->manufacture = 'ferrari';
$car->save();

So you really need to use the model to insert data/record if you want the created_at and updated_at field to be set properly.

1 comments:

This comment has been removed by the author.


EmoticonEmoticon