php - Get number of rows in Laravel's Eloquent before using "take" and "skip" -
i want query laravel model using eloquent results may need match clauses, take
, skip
predefined numbers.
this isn't problem in itself, need know number of rows found in query before reducing result set take , skip - original number of matches every row in table if no clauses used or few if either used.
what want accomplished making query twice, first omitting "->take($idisplaylength)->skip($idisplaystart)
" @ end , counting that, seems messy.
any thoughts?
$contacts = contact::where(function($query) use ($request) { if (!empty($request['firstname'])) { $query->where(function($query) use ($request) { $query->where('firstname', 'like', "%{$request['firstname']}%"); }); } if (!empty($request['lastname'])) { $query->where(function($query) use ($request) { $query->where('lastname', 'like', "%{$request['lastname']}%"); }); } }) ->take($idisplaylength)->skip($idisplaystart)->get(); $itotalrecords = count($contacts);
you can use count
get
on same query.
and way, whole query bit on complicated. results in this:
select * `contacts` ((`firstname` ?) , (`lastname` ?)) limit x, y
closure in where
used make query example:
select * table (x or y) , (a or b);
so sum need this:
$query = contact::query(); if (!empty($request['firstname'])) { $query->where('firstname', 'like', "%{$request['firstname']}%"); } if (!empty($request['lastname'])) { $query->where('lastname', 'like', "%{$request['lastname']}%"); } $count = $query->count(); $contacts = $query->take($idisplaylength)->skip(idisplaystart)->get();
Comments
Post a Comment