Laravel Eloquent limit and offset

LaravelEloquentLimitOffset

Laravel Problem Overview


This is mine

    $art = Article::where('id',$article)->firstOrFail();
	$products = $art->products;

I just wanna take a limit 'product' This is wrong way

   $products = $art->products->offset($offset*$limit)->take($limit)->get();

Please give me a hand!

Thanks!

Laravel Solutions


Solution 1 - Laravel

skip = OFFSET
$products = $art->products->skip(0)->take(10)->get(); //get first 10 rows
$products = $art->products->skip(10)->take(10)->get(); //get next 10 rows

From laravel doc 5.2 https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset

> skip / take > > To limit the number of results returned from the query, or to skip a > given number of results in the query (OFFSET), you may use the skip > and take methods: > > $users = DB::table('users')->skip(10)->take(5)->get();

In laravel 5.3 you can write (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)

$products = $art->products->offset(0)->limit(10)->get(); 

Solution 2 - Laravel

Quick:

Laravel has a fast pagination method, paginate, which only needs to pass in the number of data displayed per page.


//use the paginate
Book::orderBy('updated_at', 'desc')->paginate(8);

how to customize paging:

you can use these method: offsetlimit ,skiptake

  • offset,limit : where does the offset setting start, limiting the amount of data to be queried

  • skip,take: skip skips a few pieces of data and takes a lot of data

for example:


Model::offset(0)->limit(10)->get();

Model::skip(3)->take(3)->get();


//i use it in my project, work fine ~

class BookController extends Controller
{
    public function getList(Request $request) {

        $page = $request->has('page') ? $request->get('page') : 1;
        $limit = $request->has('limit') ? $request->get('limit') : 10;

        $books = Book::where('status', 0)->limit($limit)->offset(($page - 1) * $limit)->get()->toArray();

        return $this->getResponse($books, count($books));
    }
}


Solution 3 - Laravel

laravel have own function skip for offset and take for limit. just like below example of laravel query :-

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

Solution 4 - Laravel

You can use skip and take functions as below:

$products = $art->products->skip($offset*$limit)->take($limit)->get();

// skip should be passed param as integer value to skip the records and starting index

// take gets an integer value to get the no. of records after starting index defined by skip

EDIT

Sorry. I was misunderstood with your question. If you want something like pagination the forPage method will work for you. forPage method works for collections.

REf : https://laravel.com/docs/5.1/collections#method-forpage

e.g

$products = $art->products->forPage($page,$limit);

Solution 5 - Laravel

Maybe this

$products = $art->products->take($limit)->skip($offset)->get();

Solution 6 - Laravel

Try this sample code:

$art = Article::where('id',$article)->firstOrFail();

$products = $art->products->take($limit);

Solution 7 - Laravel

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);

$chunk = $collection->forPage(2, 3);

$chunk->all();

Solution 8 - Laravel

Please try like this,

return Article::where('id',$article)->with(['products'=>function($products, $offset, $limit) {
    return $products->offset($offset*$limit)->limit($limit);
}])

Solution 9 - Laravel

Laravel 5.1< (current v 9)

$art->products->skip($offset)->take($limit)->all();

https://laravel.com/docs/9.x/collections#method-take

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSang TrầnView Question on Stackoverflow
Solution 1 - LaravelAtiqurView Answer on Stackoverflow
Solution 2 - Laravelwater_ak47View Answer on Stackoverflow
Solution 3 - Laravelnageen nayakView Answer on Stackoverflow
Solution 4 - LaravelAliView Answer on Stackoverflow
Solution 5 - LaravelSlmmgddView Answer on Stackoverflow
Solution 6 - LaravelPramodya AbeysingheView Answer on Stackoverflow
Solution 7 - LaravelByron Jester ManaloView Answer on Stackoverflow
Solution 8 - LaravelNisamView Answer on Stackoverflow
Solution 9 - LaravelSadeeView Answer on Stackoverflow