el valuta
el valuta

Reputation: 317

Timestamps (updated_at, created_at) are null in Laravel 5

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.

Upvotes: 14

Views: 49016

Answers (6)

Victor S.
Victor S.

Reputation: 49

The columns shouldn't auto-initialize at the database level. That is handled by Eloquent


Do not use

$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrentOnUpdate();

TLDR;

  • You can't call CURRENT_TIMESTAMP twice in a table
  • You are required to declare a default value or change the column to be nullable

See:

Upvotes: 0

Rakesh Nandi
Rakesh Nandi

Reputation: 149

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

Upvotes: 15

Saumya Rastogi
Saumya Rastogi

Reputation: 13703

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..!!

Upvotes: 1

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41360

Check if your model has this line.

public $timestamps = false;

If it has, delete it.

Upvotes: 10

overburn
overburn

Reputation: 1234

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

Upvotes: 5

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

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()
            ]);

Upvotes: 12

Related Questions