我想将合同模型的每个属性复制到另一个模板合同模型中,依此类推。我的代码可以工作,但我对 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 关注
- 87 浏览
添加回答
举报
0/150
提交
取消