Why does Laravel 5 automatically update my date field?

I want to update the ( status ) field on the model. I would extract from the DB and assign a new value to the status column. But after saving the model, I saw another date field ( published_at ), also changing the same way as updated_at . The action is performed when the user clicks on the link as http: // localhost / dashboard / gallery / publish / 1 .

I do not know why the published_at update is updated automatically and is the same as updated_at ?

Here is the controller code :

 <?php class GalleryController extends Controller { /** * Approve to publish the gallery on the web. * * @param int $id * @return Response */ public function getPublish($id) { $gallery = Gallery::whereId($id)->firstOrFail(); $gallery->status = Gallery::STT_PUBLISH; $gallery->save(); return redirect( route('backend::gallery.edit',[$gallery->id]) )->with('status', 'Done'); } } ?> 

and Gallery Model :

 <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Gallery extends Model { use SoftDeletes; protected $table = 'gallery'; protected $fillable = ['title', 'slug', 'content', 'category_id', 'type']; protected $guarded = ['published_at','creator_id','thumbnail']; protected $dates = ['deleted_at', 'published_at']; } ?> 

and migration function :

 public function up() { Schema::create('gallery', function (Blueprint $table) { $table->increments('id')->unsigned(); $table->string('slug')->unique(); $table->tinyInteger('type')->unsigned(); $table->integer('category_id')->unsigned()->default(0); $table->string('thumbnail'); $table->string('title'); $table->text('content'); $table->integer('status')->unsigned()->default(0); $table->timestamp('published_at'); $table->integer('creator_id')->unsigned(); $table->timestamps(); $table->index(['slug']); $table->softDeletes(); }); Schema::table('gallery', function (Blueprint $table) { $table->foreign('category_id')->references('id')->on('category'); $table->foreign('creator_id')->references('id')->on('users'); }); } 

UPDATE

I see that this configuration in the Migration function makes the problem, here: $table->timestamp('published_at');

And this statement creates the following SQL:

 CREATE TABLE `gallery` ( ... `published_at` Timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP, ... ) 

So, how to configure the timestamp field, which is not an automatic update at the current time?

UPDATE 2

Done, I'm changing the wrapping, just use dateTime instead of timestamp .

Use it, $table->dateTime('published_at'); , this statement will not be automatically updated at CURRENT_TIMESTAMP.

+5
source share
3 answers

It seems like it happens that MySQL (or any database you are using) is updating the date, not Laravel.

You need to change:

 $table->timestamp('published_at'); 

in

 $table->dateTime('published_at'); 

Then do php artisan migrate:refresh to roll back and recreate your tables.

+3
source

This mail is old, but for those who received this problem, this is the easiest solution. In your migration, the timestamp field just needs to be null. Then in this field there will be no automatic update trigger.

 $table->timestamp('published_at')->nullable(); 

No need to use $table->dateTime() instead of $table->timestamp() .

+2
source

An alternative solution is to create a migration that runs this query to remove the ON UPDATE column trigger:

ALTER TABLE queued_posts CHANGE scheduled_publish_time scheduled_publish_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP

-1
source

Source: https://habr.com/ru/post/1244338/


All Articles