Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Laravel 5 auto update my date field?

I want to update a field (status) on the model. I would retrieve from DB and assign a new value to status column. But after the model saved, I saw another date field (published_at) also changed to same as updated_at. The action run when user click on a link as http://localhost/dashboard/gallery/publish/1.

I don't know why the published_at updated auto and same as updated_at?

Here is 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 func:

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 this config in Migration function makes problem, here: $table->timestamp('published_at');

And this statement creates SQL like that:

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

So, how to set up a timestamp field, which is not auto update on current time?

UPDATE 2

Done, I modify the migration, just use dateTime instead of timestamp.

Use it, $table->dateTime('published_at');, this statement will not auto update on CURRENT_TIMESTAMP.

like image 735
Davuz Avatar asked Mar 03 '16 08:03

Davuz


People also ask

How do I change the date field in laravel?

use Carbon\Carbon; $date = "02 08 2012 10:09PM"; list($month, $day, $year, $time) = explode(' ', $date); $updateData = [ 'status' => 1, 'datetime' => Carbon::parse("$month-$day-$year $time")->toDateTimeString(), // 2012-08-02 22:09:00 ]; $data = Attendance::where('status', '0')->update($updateData);

What is the data type for date in laravel?

It is stored in the database as YYYY-MM-DD for MySql.


2 Answers

It looks like what is happening is that MySQL (or whichever database you are using) is updating the date, not Laravel.

You need to change:

$table->timestamp('published_at');

to:

$table->dateTime('published_at');

Then do a php artisan migrate:refresh to rollback and recreate your tables.

like image 131
Joseph Avatar answered Oct 04 '22 22:10

Joseph


This Post is old but for anyone who got this problem here is the simplest solution. In your migration the timestamp field just has to be nullable. Than there will no auto update trigger on this field.

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

No need for using $table->dateTime() instead of $table->timestamp().

like image 24
Tobias Schulz Avatar answered Oct 05 '22 00:10

Tobias Schulz