douye9822 2018-09-09 15:06
浏览 90
已采纳

Laravel 5.6更改时间戳

A noob question: I have not been successful changing my timestamps in Laravel. I even went into my Model.php to change my timestamps but still not successful. Hope someone can guide me on this matter. As I would like to change my timestamp to:

const CREATED_AT = 'creation_date';

const UPDATED_AT = 'last_update';

Below is the my Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Events extends Model
{

    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';

    public function event_type(){
        return $this->belongsTo('App\EventsType');
    }


    public function time(){
        $this->hasMany('App\Time');
    }

    // protected $fillable = [

    //     'name', 'description','date_of_event','location','is_Active',

    // ];

}

My migration is below:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEventsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('event_id');
            $table->string('name');
            $table->text('description');
            $table->datetime('date_of_event');
            $table->timestamps();
            $table->text('location');
            $table->text('note');
            $table->boolean('is_Active');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('events');
    }
}
  • 写回答

2条回答 默认 最新

  • doujianzi8521 2018-09-09 15:15
    关注

    Laravel provides timestamp management automatically You just have to include this at the end of your schema create method

    $table->timestamps();
    

    this will give you two columns in table 'created_at' and 'updated_at'. they are self explanatory. they are automatically managed when you create or update any record.

    if you want to manage timestamps on your own then

    $table->datetime('creation_date');
    $table->datetime('last_update');
    

    in this case you have to fill these values manually when you create or update records.

    Hope this clears your concept.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿