清除 Laravel 的 orderBy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26869897/
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
Clear Laravel's orderBy
提问by Rafael Sierra
I have a generic function which gives me generic querysets, something like:
我有一个通用函数,它为我提供通用查询集,例如:
class Model extends Eloquent {
public static function get_queryset(){
$queryset = self::where('foo','=','bar');
// Do tons of stuff with the query and then...
return $queryset->orderBy('somefield');
}
}
This function is used everywhere my project, but in a specific point I need to use this queryset but changingthe ORDER BY, much like this:
这个函数在我的项目的任何地方都使用,但在特定点我需要使用这个查询集但改变ORDER BY,就像这样:
public static function get_specific_field(){
return self::get_queryset()->select('singlefield')->orderBy('singlefield');
}
If I run this code, the ORDER BY will just append onto the previous and generate an invalid query, since the "somefield" is not on the SELECTed fields. i.e.:
如果我运行此代码,则 ORDER BY 只会附加到前一个并生成无效查询,因为“somefield”不在 SELECTed 字段上。IE:
SELECT singlefield FROM table ORDER BY somefield ASC, singlefield ASC
How do I clear the orderBy so I can just reuse querysets?
如何清除 orderBy 以便我可以重用查询集?
回答by jpschroeder
I agree there should be a clearOrderBy()
method added to the query builder. However because the registry of orderbys is a public property on Illuminate\Database\Query\Builder
you actually can clear it yourself today. The trick is getting access to the base query object:
我同意应该clearOrderBy()
向查询构建器添加一个方法。但是,因为 orderbys 的注册表是您的公共财产,所以Illuminate\Database\Query\Builder
您今天实际上可以自己清除它。诀窍是访问基本查询对象:
$query = YourModel::where('status', 1)->orderBy('created_at','desc');
// ... lots of other code, something needs to reset the order by ...
$query->getQuery()->orders = null;
$query->orderBy('other_column', 'desc');
At other times, for example when manipulating a relation query, you need to get to access the base query (Illuminate\Database\Query\Query
). So for example:
在其他时候,例如在操作关系查询时,您需要访问基本查询 ( Illuminate\Database\Query\Query
)。例如:
$query = YourModel::find(1)->load('children', function ($query) {
$query->getBaseQuery()->orders = null;
});
Thats it. I plan to submit a PR for a clearOrderBy()
as well.
就是这样。我也打算为 a 提交一个 PR clearOrderBy()
。
回答by Mark Baker
Why not "genericise" your queryset?
为什么不“通用化”您的查询集?
class Model extends Eloquent {
public static function get_queryset($orderBy = 'somefield'){
$queryset = self::where('foo','=','bar');
// Do tons of stuff with the query and then...
return $queryset->orderBy($orderBy);
}
}
Then you can use
然后你可以使用
public static function get_specific_field(){
return self::get_queryset('singlefield')->select('singlefield');
}
回答by Bader
Here is a simple solution , tiny package you can use it by simply calling ->clearOrdersBy()
https://github.com/phpfalcon/laravel-clear-orders-by
这是一个简单的解决方案,您可以通过简单地调用https://github.com/phpfalcon/laravel-clear-orders-by来使用它的小包->clearOrdersBy()