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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?