Laravel 查询构建器 - 使用修改后的 where 语句重用查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30235551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:33:04  来源:igfitidea点击:

Laravel query builder - re-use query with amended where statement

phpmysqllaravelquery-builder

提问by Geoff Clayton

My application dynamically builds and runs complex queries to generate reports. In some instances I need to get multiple, somewhat arbitrary date ranges, with all other parameters the same.

我的应用程序动态构建和运行复杂的查询以生成报告。在某些情况下,我需要获得多个、有点任意的日期范围,所有其他参数都相同。

So my code builds the query with a bunch of joins, wheres, sorts, limits etc and then runs the query. What I then want to do is jump into the Builder object and change the where clauses which define the date range to be queried.

所以我的代码使用一堆连接、位置、排序、限制等构建查询,然后运行查询。然后我想做的是跳转到 Builder 对象并更改定义要查询的日期范围的 where 子句。

So far, I have made it so that the date range is setup before any other wheres and then tried to manually change the value in the relevant attribute of the wheres array. Like this;

到目前为止,我已将日期范围设置在任何其他 wheres 之前,然后尝试手动更改 wheres 数组的相关属性中的值。像这样;

$this->data_qry->wheres[0]['value'] = $new_from_date;
$this->data_qry->wheres[1]['value'] = $new_to_date;

Then I do (having already done it once already)

然后我做(已经做过一次了)

$this->data_qry->get();

Doesn't work though. The query just runs with the original date range. Even if my way worked, I still wouldn't like it though as it seems to be shot through with a precarious dependence (some sort of coupling?). Ie; if the date wheres aren't set up first then it all falls apart.

虽然不起作用。查询仅在原始日期范围内运行。即使我的方法奏效了,我仍然不喜欢它,因为它似乎被一种不稳定的依赖(某种耦合?)击穿了。IE; 如果没有先设置日期,那么一切都会分崩离析。

I couldset the whole query up again from scratch, just with a different date range, but that seems ott as everything else in the query needs to be the same as the previous time it was used.

可以从头开始重新设置整个查询,只是使用不同的日期范围,但这似乎很奇怪,因为查询中的其他所有内容都需要与上次使用时相同。

Any ideas for how to achieve this in the correct / neatest way are very welcome.

任何关于如何以正确/最简洁的方式实现这一目标的想法都非常受欢迎。

Thanks,

谢谢,

Geoff

杰夫

回答by lukasgeiter

You can use cloneto duplicate the query and then run it with different where statements. First, build the query without the from-to constraints, then do something like this:

您可以使用clone复制查询,然后使用不同的 where 语句运行它。首先,在没有 from-to 约束的情况下构建查询,然后执行以下操作:

$query1 = $this->data_qry;
$query2 = clone $query1;

$result1 = $query1->where('from', $from1)->where('to', $to1)->get();
$result2 = $query2->where('from', $from2)->where('to', $to2)->get();

回答by Mark Boots

The suggestion from @lukasgeiter using clone is definitely the way to go; the reason is that an Eloquent\Builder object contains an internal reference to a Query\Builder that needs to be duplicated.

@lukasgeiter 使用 clone 的建议绝对是可行的方法;原因是 Eloquent\Builder 对象包含对需要复制的 Query\Builder 的内部引用。

To keep the flow of your app and get back to a more functional style, you can use Laravel's with()helper, which simply returns the object passed in:

为了保持应用程序的流畅性并恢复到更实用的风格,您可以使用 Laravel 的with()助手,它只返回传入的对象:

$result1 = with(clone $this->data_qry)->where('from', $from1)->where('to', $to1)->get();
$result2 = with(clone $this->data_qry)->where('from', $from2)->where('to', $to2)->get();