首页 > 解决方案 > 雄辩在哪里堆积

问题描述

我的问题如下图

$data = Model::whereRaw(<condition 1>)
$data1 = $data->whereRaw(<condition 2>)
$data2 = $data->whereRaw(<condition 3>)

每当我打印时,$data2->toSql()我都会得到这个

select * from table where <condition 1> and <condition 2> and <condition 3>

代替

select * from table where <condition 1> and <condition 2>

标签: phplaraveleloquent

解决方案


因为 $data, $data1,$data2 指向内存中同一个对象的指针......

解决这个问题:

$data = Model::whereRaw(<condition 1>);

$data1 =(clone $data)->whereRaw(<condition 2>)

$data2 = (clone $data)->whereRaw(<condition 3>)

推荐阅读