84

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!

Sang Trần
  • 1,517
  • 3
  • 10
  • 18

9 Answers9

149
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(); 
Atiqur
  • 2,879
  • 1
  • 21
  • 25
16

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


water_ak47
  • 616
  • 6
  • 8
10

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();
nageen nayak
  • 1,076
  • 2
  • 15
  • 21
5

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);
Ali
  • 1,260
  • 9
  • 14
2

Maybe this

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

Salem Megiddo
  • 165
  • 12
0

Try this sample code:

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

$products = $art->products->take($limit);
Brane
  • 2,781
  • 2
  • 31
  • 51
0
$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);

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

$chunk->all();
vimuth
  • 2,928
  • 13
  • 43
  • 73
0

Laravel 8 (This is worked for the version 7.16)

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

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

Sadee
  • 2,280
  • 27
  • 33
-1

Please try like this,

return Article::where('id',$article)->with(['products'=>function($products, $offset, $limit) {
    return $products->offset($offset*$limit)->limit($limit);
}])
Nisam
  • 2,245
  • 19
  • 31
  • Thanks, Nisam but it not work. in my DB, an article has many products through 'article_product' table. I can show all products (e.g which is related with article_id =1 ) – Sang Trần Feb 26 '16 at 04:27