亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 模型 whereOrCreate

Laravel 模型 whereOrCreate

PHP
楊魅力 2023-07-21 14:15:02
我知道使用 Model::firstOrCreate、updateOrCreate 在不滿足條件時如何創建模型實例。像這樣的東西:Model::where('number', $num)->orCreate(['number' => $num, 'another' => $another]);我正在嘗試通過 Excel 文件將信息存儲到表中,但其中一個字段是唯一的,請問我該如何處理?
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

這正是firstOrCreate()可以用來做的事情。

Model::firstOrCreate(
    ['number' => $num],
    ['another' => $another]
);

會找到一個模型['number' => $num],其中 ,如果沒有找到,它將創建一個新模型['number' => $num]  ['another' => $another]

https://laravel.com/docs/7.x/eloquent#other-creation-methods

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

查看完整回答
反對 回復 2023-07-21
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號