我正在尝试复制表行及其关系。
但我收到了replicate()不存在的错误消息,
我在stackoverflow上看到很多人使用replicate()没有任何问题,但是我得到了这个错误
我的控制器代码
public function copyshowtime($cinema_id,$show_date)
{
$date=new Carbon($show_date);
$current_show_date=$date->format('Y-m-d');
$next_show_date=$date->addDay()->format('Y-m-d');
$movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->get();
$newshowtime=$movieshowtime->replicate();
return $newshowtime;
}
有没有任何名称空间我必须使用replicate(),我无法从laravel网站也得到解决方案。
请帮忙。
4条答案
按热度按时间dkqlctbz1#
您可以在模型上使用
replicate()
,但不能在集合上使用。通过使用
get()
获取记录,您将返回一个集合。如果您只希望返回一条记录,则将
get()
替换为first()
,然后replicate()
应该存在,因为它将返回模型的示例而不是集合:您还将需要
save()
$newshowtime
。lymnna712#
这段代码对我来说非常有效
如有任何改进此代码的建议,我们将不胜感激。
eulz3vhy3#
这种类型的函数将有助于克隆多个记录,并将这些记录添加到同一个表中。我尝试了类似的代码流,并成功了。
干杯!干杯!
iqxoj9l94#
您可以轻松地复制具有新更改的行
如果不使用toArray(),则foreach循环中的每个元素都将是Eloquent对象。