Laravel Eloquent ORM 复制
本文介绍了Laravel Eloquent ORM 复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用所有关系复制我的模型之一时遇到问题.
I have a problem with replicating one of my models with all the relationships.
数据库结构如下:
关系是:
- product hasMany product_options
- 产品属于多类别(通过 product_categories)
我想克隆一个具有所有关系的产品.目前这是我的代码:
I would like to clone a product with all the relationships. Currently here is my code:
但这不起作用(关系未克隆 - 目前我只是尝试克隆 product_options).
But this does not works (the relationships are not cloned - currently I just tried to clone the product_options).
推荐答案
这段代码,对我有用:
从这里回答:克隆一个包含所有关系的 Eloquent 对象?
这个答案(同样的问题),也很好用.
This answer (same question), also works fine too.
从这里:克隆一个包含所有关系的 Eloquent 对象?
根据我的经验,该代码适用于多对多关系.
The code works fine for many to many relationships in my experience.
这篇关于Laravel Eloquent ORM 复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!