讓我以此帖子開(kāi)頭,我無(wú)法更改插入主鍵的方法。這是在遺留系統(tǒng)上開(kāi)發(fā)的,我無(wú)法控制檢索主鍵的方法,我只需要處理它。我發(fā)現(xiàn)Laravel在使用create方法時(shí)不會(huì)使用集合主鍵值的觀察者來(lái)更新集合主鍵。這是我的情況(我縮小了模型和文件的空間):遷移文件:Schema::create('forms_maps', function (Blueprint $table) { $table->integer('id')->unsigned(); $table->string('name');});ModelObserver.php:public function creating(Model $model){ $countername = strtolower(class_basename(get_class($model))).'s_id'; $model->id = tap(\App\Models\OCounter::where('countername',$countername)->first())->increment('counterval')->fresh()->counterval; Log::debug("Creating ". strtolower(class_basename(get_class($model))) . ": " . $model);}DatabaseSeeder.php:$accountApp = \App\Models\FormsMap::create(['name' => 'Account Application']);Log::debug("Created formsmap: " . $accountApp);輸出日志:Creating formsmap: {"name":"Account Application","id":84}Created formsmap: {"name":"Account Application","id":0}從日志中可以看到,當(dāng)使用create方法創(chuàng)建記錄時(shí),在觀察者內(nèi)部,我得到了正確的id;但是,該值不會(huì)傳遞回DatabaseSeeder中的集合。我看錯(cuò)了嗎?我是否應(yīng)該使用其他方法將值插入表中?我不想手動(dòng)/內(nèi)聯(lián)插入此值,因?yàn)槊總€(gè)模型都必須注入此信息。謝謝!
1 回答

Helenr
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
哎呀!!!! 我每次都這樣做!答案是:
該模型需要關(guān)閉增量功能。
class FormsMap extends Model
{
public $timestamps = false;
public $incrementing = false;
...
}
!
- 1 回答
- 0 關(guān)注
- 205 瀏覽
添加回答
舉報(bào)
0/150
提交
取消