php - Clear Laravel's orderBy -
i have generic function gives me generic querysets, like:
class model extends eloquent { public static function get_queryset(){ $queryset = self::where('foo','=','bar'); // tons of stuff query , then... return $queryset->orderby('somefield'); } }
this function used everywhere project, in specific point need use queryset changing order by, this:
public static function get_specific_field(){ return self::get_queryset()->select('singlefield')->orderby('singlefield'); }
if run code, order append onto previous , generate invalid query, since "somefield" not on selected fields. i.e.:
select singlefield table order somefield asc, singlefield asc
how clear orderby can reuse querysets?
i agree there should clearorderby()
method added query builder. because registry of orderbys public property on illuminate\database\query\builder
can clear today. trick getting access base query object:
$query = yourmodel::where('status', 1)->orderby('created_at','desc'); // ... lots of other code, needs reset order ... $query->getquery()->orders = null; $query->orderby('other_column', 'desc');
at other times, example when manipulating relation query, need access base query (illuminate\database\query\query
). example:
$query = yourmodel::find(1)->load('children', function ($query) { $query->getbasequery()->orders = null; });
thats it. plan submit pr clearorderby()
well.
Comments
Post a Comment