我想將合同模型的每個屬性復制到另一個模板合同模型中,依此類推。我的代碼可以工作,但我對 laravel (或者實際上是 php)了解不多,我的直覺告訴我必須有更好的方法,或更優雅的方法。fill()Laravel 并沒有變得更好。也許有一個構造函數?相等表:合同 -> 模板合同章節 -> 模板章節條款 -> 模板條款public function storeTemplate(ContractCreateRequest $request) { DB::beginTransaction(); try { $contract = Contract::find($request->input()['type']); $templatecontract = new TemplateContract(); $templatecontract->id = $contract->id; $templatecontract->pid = $contract->pid; $templatecontract->deleted = $contract->deleted; $templatecontract->sorting = $contract->sorting; $templatecontract->created_at = $contract->created_at; $templatecontract->updated_at = $contract->updated_at; $templatecontract->deleted_at = $contract->deleted_at; $templatecontract->title = $contract->title; $templatecontract->description = $contract->description; $templatecontract->hidden = $contract->hidden; $templatecontract->contract_type = $contract->contract_type; $templatecontract->process_type = $contract->process_type; $templatecontract->tstamp = $contract->tstamp; $templatecontract->is_english = $contract->is_english; $templatecontract->usecasetitle = $contract->usecasetitle;
1 回答

飲歌長嘯
TA貢獻1951條經驗 獲得超3個贊
props
因此,請求中的幾乎所有內容都與列名稱匹配。你只需要調用Eloquentcreate
模型的方法來創建一條新記錄并傳遞鍵值對而不是.?另外,您不必擔心包含的額外參數,因為只會提取和分配類屬性中定義的參數。object
$contract
Laravel
protected $fillable
// cast object to array
$contract = (array) $contract;
$templateContract = TemplateContract::create($contract)
- 1 回答
- 0 關注
- 118 瀏覽
添加回答
舉報
0/150
提交
取消