Php 雄辩的堆砌在哪里
我的问题如下所示Php 雄辩的堆砌在哪里,php,laravel,eloquent,Php,Laravel,Eloquent,我的问题如下所示 $data = Model::whereRaw(<condition 1>) $data1 = $data->whereRaw(<condition 2>) $data2 = $data->whereRaw(<condition 3>) 而不是 select * from table where <condition 1> and <condition 2> 从表中选择*,其中和 因为$data、$da
$data = Model::whereRaw(<condition 1>)
$data1 = $data->whereRaw(<condition 2>)
$data2 = $data->whereRaw(<condition 3>)
而不是
select * from table where <condition 1> and <condition 2>
从表中选择*,其中和
因为$data、$data1、$data2指针指向内存中的同一对象
要解决这个问题:
$data = Model::whereRaw(<condition 1>);
$data1 =(clone $data)->whereRaw(<condition 2>)
$data2 = (clone $data)->whereRaw(<condition 3>)
$data=Model::whereRaw();
$data1=(克隆$data)->whereRaw()
$data2=(克隆$data)->whereRaw()
尝试
$data = Model::whereRaw(<condition 1>);
$data_cloned = $data->replicate();
$data1 = $data->whereRaw(<condition 2>);
$data2 = $data_cloned->whereRaw(<condition 3>);
return $data1->toSql();
$data=Model::whereRaw();
$data_cloned=$data->replicate();
$data1=$data->whereRaw();
$data2=$data_cloned->whereRaw();
返回$data1->toSql();
方法whereRaw()
返回类实例(方法链接),因此$data
,$data1
和$data2
包含对同一对象的引用。无需克隆两次,您可以将原始变量用于最后一个条件。请在您的答案中添加一些解释,以便其他人可以从中学习。这只是一个按引用传递和传递值的原则。由于模型返回的是引用而不是值,因此必须复制引用以避免重写值$data->replicate()
表示引用分离。请通过编辑将所有解释添加到您的答案中
$data = Model::whereRaw(<condition 1>);
$data_cloned = $data->replicate();
$data1 = $data->whereRaw(<condition 2>);
$data2 = $data_cloned->whereRaw(<condition 3>);
return $data1->toSql();