我需要獲得新創建記錄的模型
$data = [['name'/span> => 'john'], ['name' => 'Doe'] ]。
$result = Model::insert($data)。
但是當我檢查$result變數時,它是布林值。
我怎樣才能得到新創建的記錄呢?
uj5u.com熱心網友回復:
使用fresh()解決了我的問題
$data = [['name' => 'john'], ['name' => 'Doe'] ]。
$result = Model::create($data)。
$data = $result-> fresh();
uj5u.com熱心網友回復:
你可以不使用insert,而使用create,例如,
$data = [['name' => 'john'], ['name' => 'Doe'] ]。
$result = Model::insert($data); //instead of this!
$result = Model::create($data);//使用這個,。
那么$result關鍵字將回傳最后創建的資料。
如果你使用的是
$result = $model->save();方法,你必須回傳$model變數。 不應該回傳$result,因為它只會給你一個布林值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324238.html
標籤:
上一篇:在"mongosh"中,我如何在不洗掉典型的"admin"、"config"或"local"資料庫的情況下洗掉所有資料庫
