欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

據(jù)官方文檔的說(shuō)明,使用Eloquent ORM,插數(shù)據(jù)庫(kù)的時(shí)候可以自動(dòng)生成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ù)庫(kù)中created_at和updated_at字段為0000-00-00 00:00:00。

原因分析:原生的插入語(yǔ)句,Laravel是不會(huì)自動(dòng)幫你插入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的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論