最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題

 更新時間:2019年10月11日 10:01:56   作者:lanwithyu  
今天小編就為大家分享一篇解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

據(jù)官方文檔的說明,使用Eloquent ORM,插數(shù)據(jù)庫的時候可以自動生成created_at,updated_at,代碼如下:

Model里的代碼:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Notice extends Model
{
  protected $guarded = [];

  //獲取部門名稱
  public function fromDep(){
    return $this->belongsTo('App\Models\Department','from','id');
  }

  public function toDep(){
    return $this->belongsTo('App\Models\Department','to','id');
  }

  public function toUser(){
    return $this->belongsTo('App\User','create_user','id');
  }
}

新增的代碼

public function store(Request $request)
  {
    $data = $request->only(['title','sort','level','from','content','document']);
    $data['creater'] = Auth::user()->id;
    if(Notice::insert($data)){
      return ResponseLayout::apply(true);
    }else{
      return ResponseLayout::apply(false);
    }
  }

插入一條數(shù)據(jù),數(shù)據(jù)庫中created_at和updated_at字段為0000-00-00 00:00:00。

原因分析:原生的插入語句,Laravel是不會自動幫你插入created_at和updated_at字段的。

解決方法

create

public function store(Request $request)
  {
    $data = $request->only(['title','sort','level','from','content','document']);
    $data['creater'] = Auth::user()->id;
    if(Notice::create($data)){
      return ResponseLayout::apply(true);
    }else{
      return ResponseLayout::apply(false);
    }
  }

save

public function store(Request $request)
  {
    $data = $request->only(['title','sort','level','from','content','document']);
    $data['creater'] = Auth::user()->id;
    $notice = new Notice($data);
    if($notice->save()){
      return ResponseLayout::apply(true);
    }else{
      return ResponseLayout::apply(false);
    }
  }

以上這篇解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

乌拉特后旗| 桂林市| 沭阳县| 二连浩特市| 铁岭县| 襄樊市| 柘荣县| 蓝山县| 同江市| 富川| 台南县| 文安县| 温宿县| 阿坝县| 千阳县| 桃园市| 左贡县| 项城市| 浦城县| 澎湖县| 孟州市| 海淀区| 红原县| 布尔津县| 北辰区| 崇左市| 深州市| 清流县| 白朗县| 徐汇区| 濮阳县| 牟定县| 汤阴县| 湄潭县| 太谷县| 奉贤区| 九江市| 乐业县| 鲁甸县| 晋州市| 扎囊县|