婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

熱門標簽:辦理重慶400電話 智能電話機器人好公司門薩維 銅川電話機器人價格 AI電話機器人OEM貼牌 沛縣400電話辦理 青白江地圖標注 江蘇電商外呼系統(tǒng)運營商 聊城電話外呼系統(tǒng)公司 德陽中江如何申請400開頭電話

據(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的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Laravel timestamps 設置為unix時間戳的方法
  • laravel model模型定義實現(xiàn)開啟自動管理時間created_at,updated_at

標簽:赤峰 南寧 迪慶 鷹潭 山南 濟寧 三亞 烏魯木齊

巨人網(wǎng)絡通訊聲明:本文標題《解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題》,本文關鍵詞  解決,Laravel,使用,insert,插入,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題》相關的同類信息!
  • 本頁收集關于解決Laravel 使用insert插入數(shù)據(jù),字段created_at為0000的問題的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 荥阳市| 东兰县| 泸定县| 深州市| 灵寿县| 浦东新区| 丰宁| 喜德县| 桦甸市| 白河县| 苗栗市| 南雄市| 罗江县| 右玉县| 丹凤县| 元氏县| 旺苍县| 印江| 潍坊市| 仲巴县| 运城市| 常熟市| 新巴尔虎左旗| 泗洪县| 梅州市| 冕宁县| 工布江达县| 阿合奇县| 金山区| 永靖县| 湘乡市| 屯留县| 揭阳市| 社会| 九江县| 武宁县| 乌拉特中旗| 会理县| 广河县| 海口市| 阿尔山市|