0
votes

You have a database of 10,000 customers. You want to limit your search to only 500 of those customers but want to paginate the results by 50 results each page. So the last page, page 10, of the results would have 50 results. There would be no page 11.

Is this possible with eloquent/laravel? Something like "select * from people order by field desc limit 500" and then perform pagination on those results?

Paginate seems to use ->take() and ->offset(), which ignore ->limit(). I'd like to be able to use all three.

3
maybe you could do the 500 limit as a subquery and then paginate the main query?Snapey

3 Answers

2
votes

With a subquery

        $events = DB::table(function ($query) {
            $query->selectRaw('*')
                ->from('system_events_collected')
                ->orderBy('event_at','desc')
                ->take(500);
        })->paginate(10);

in this case, the data I want to paginate is in a table called system_events_collected. A subquery gets the first 500 rows from this table, then pagination is performed on the results.

-1
votes

Try this :

Article::where([['user_id','=',auth()->user()->id]])
            ->where([['title','LIKE',"%".$text_val."%"]])
            ->orderBy('id','DESC')
            ->get();

Or use "DB"