Home > Back-end >  How I get data like 0 to 50 and 51 to 100 in laravel
How I get data like 0 to 50 and 51 to 100 in laravel

Time:07-18

I want to get data from database with range like 0 to 50 and 51 to 100, this is possible? if possible please talk me how to do that.

$users = User::all();

CodePudding user response:

I am not able to get clearly what do you want. But I have provided some examples below that might work for you.

$users = DB::table('users')
           ->whereBetween('id', [1, 50])
           ->get();

Also try skip and take. You may use the skip and take methods to limit the number of results returned from the query or to skip a given number of results in the query:

$users = DB::table('users')->skip(50)->take(50)->get();

Alternatively, you may use the limit and offset methods. These methods are functionally equivalent to the take and skip methods, respectively:

$users = DB::table('users')
                ->offset(50)
                ->limit(50)
                ->get();

Hope this helps!

CodePudding user response:

There are a number of options available to you. If you're looking to obtain a working data set of x results at a time and not concerned about the id of the results, then one of the following might be of use:

Both of the above will allow you to define the size/number of records returned for your data set.

If you're looking to return records between specific id values, then you'll want to use something like whereBetween on your database queries. The whereBetween clause can be used in conjunction with the pagination and chunking.

  • Related