有没有办法在雄辩的Laravel中使用先前的查询条件进一步查询?(Is there a way to further query using the previous query conditions

编程入门 行业动态 更新时间:2024-10-14 10:45:58
有没有办法在雄辩的Laravel中使用先前的查询条件进一步查询?(Is there a way to further query using the previous query conditions in eloquent of Laravel?)

假设我有如下查询:

$firstQueryResult = ItemModel::where('type', 'myType'); $firstQueryResult = $firstQueryResult->where('size', '>', '10'); $firstQueryResult = $firstQueryResult->where('price', '>', '10'); //many other condition..... $firstQueryResult = $firstQueryResult->paginate(10);

现在,我有一个非常相似的查询,它基本上与第一个查询相同,除了一个条件,所以我尝试做这样的事情:

$firstQueryResult = ItemModel::where('type', 'myType'); $firstQueryResult = $firstQueryResult->where('size', '>', '10'); $firstQueryResult = $firstQueryResult->where('price', '>', '10'); //many other condition..... $secondQueryResult = $firstQueryResult->where('special', 'true')->count(); $firstQueryResult = $firstQueryResult->paginate(10);

当第二个查询有效时,第一个查询现在也采用第二个查询的额外条件。 我很确定即使在以后的开发中,这两个查询也会非常相似,所以为了以后更好的维护,我不想复制和粘贴第一个查询。 有没有办法重用第一个查询中设置的条件而不会搞乱第一个查询?

PS我正在使用laravel 4.2

Let say I have a query like below:

$firstQueryResult = ItemModel::where('type', 'myType'); $firstQueryResult = $firstQueryResult->where('size', '>', '10'); $firstQueryResult = $firstQueryResult->where('price', '>', '10'); //many other condition..... $firstQueryResult = $firstQueryResult->paginate(10);

Now, I have a very similar query, which basically is the same as the first query except one condition, so I tried to do something like this:

$firstQueryResult = ItemModel::where('type', 'myType'); $firstQueryResult = $firstQueryResult->where('size', '>', '10'); $firstQueryResult = $firstQueryResult->where('price', '>', '10'); //many other condition..... $secondQueryResult = $firstQueryResult->where('special', 'true')->count(); $firstQueryResult = $firstQueryResult->paginate(10);

While the second query works, first query now also take the extra condition of the second query. I am quite sure even in later development, these 2 queries will be very similar, so for better maintain in the future, I don't want to do a copy and paste of the first query. Is there a way to reuse the conditions set in first query without messing with the first query?

P.S. I am using laravel 4.2

最满意答案

您可以像这样粘合查询并重用查询部分:

public function getItem() { $query = ItemModel::where('type', 'myType'); $query = $this->queryPriceSize($query); //... paginate... } protected function queryPriceSize($query) { $query->where('size', '>', '10'); $query->where('price', '>', '10'); return $query; }

另一种选择是查询范围,如下所述: https : //laravel.com/docs/4.2/eloquent#query-scopes

Besides @herrjeh42 's answer, I found that using clone can prevent the problem too.

Using the above example:

$firstQueryResult = ItemModel::where('type', 'myType'); $firstQueryResult = $firstQueryResult->where('size', '>', '10'); $firstQueryResult = $firstQueryResult->where('price', '>', '10'); //many other condition..... $secondQueryResult = clone $firstQueryResult; $secondQueryResult = $secondQueryResult->where('special', 'true')->count(); $firstQueryResult = $firstQueryResult->paginate(10);

更多推荐

本文发布于:2023-07-16 12:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:雄辩   没有办法   先前   条件   conditions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!