12

I have a problem with updated_at, created_at fields in Laravel 5.

Here is my migration:

Schema::create('lots', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('lot');
    $table->integer('is_active');
    $table->timestamps();
});

But when I insert some data into this table, updated_at and created_at fields are null. How make them auto-complete with current timestamps?

I insert data like this:

\DB::table('admin_lots')->insert([
    'lot' => $request->cycle_lot,
    'is_active' => '1',
]);

Thanks.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
el valuta
  • 287
  • 2
  • 5
  • 14

5 Answers5

14

You have to use the create method instead of the insert method in Laravel.

The create method automatically adds timestamps for created_at and updated_at fields:

// GOOD:
User::create(array(
    'name' => 'John'
));

On the contrary, the insert method bypasses Eloquent, (it uses the query builder) and does not update updated_at/created_at columns!

// BAD:
User::insert([
    'name' => '[[ test name ]]',
]);

dd(
    User::where(['name' => '[[ test name ]]'])->first()->create_date
);

enter image description here

raveren
  • 17,799
  • 12
  • 70
  • 83
Rakesh Nandi
  • 141
  • 1
  • 5
12

You probably do not use Eloquent when inserting data, in this case you should add timestamps manually.

If you do not want to do this, but you still need filled timestamps, use this hack:

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

Update

Based on your updated code, here's another solution:

\DB::table('admin_lots')->insert([
                'lot'   => $request->cycle_lot,
                'is_active'     => '1',
                'created_at' = \Carbon\Carbon::now()->toDateTimeString(),
                'updated_at' = \Carbon\Carbon::now()->toDateTimeString()
            ]);
Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
8

Check if your model has this line.

public $timestamps = false;

If it has, delete it.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
5

When you instert data directly, Laravel won't know about your timestamps. You can either set the timestamps manually in the insert statement, or switch to using Eloquent models , which handle many things out of the box for you, including timestamps. It's also way easier to maintain than straight queries, where applicable.

Eloquent ORM

overburn
  • 1,194
  • 9
  • 27
  • 3
    I really recommend using Eloquent in this case. Anything else will result in unnecessary difficult maintainable non-laravel way code. – Pᴇʜ May 03 '16 at 08:40
1

You need to use Laravel's awesome Eloquent feature to make timestamps written to the Database automatically...

As by seeing your example the code for eloquent will go something like this:

$lot_inputs = array(
    'lot' => $request->cycle_lot,
    'is_active' => 1
);
$new_lot = Lot::create($lot_inputs);

Please note that you should have the Model for the table = 'lots' (and it must extend Eloquent) so that you can easily use Eloquent methods and its properties...

It would be great if you use Eloquent ORM as much as possible so that if in future you want to change your DB technology then you won't need to specify the written eloquent queries again (e.g: the conversion of query to different DB languages is automatically done by Eloquent)

Thanks I hope this will help you to resolve your issue..!!

Sebastian
  • 1,321
  • 9
  • 21
Saumya Rastogi
  • 13,159
  • 5
  • 42
  • 45