Laravel Eloquent limit and offset

前端 未结 9 1026
陌清茗
陌清茗 2020-12-13 05:25

This is mine

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

I just wanna take a limit \'p

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 06:16

    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);
    

提交回复
热议问题