ChatGPT解决这个技术问题 Extra ChatGPT

Laravel Eloquent limit and offset

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!

@SangTrần Just use products() instead of products. And it'll work.
As Doan Tran mentioned, you should be calling those methods on the builder, not a collection. Collections do not have a method skip.
Please add what version of Laravel you are using. There are many of them.

A
Atiqur
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(); 

w
water_ak47

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



This will return empty for pages with rows less than the limit, since its offsetting beyond the scope.
n
nageen nayak

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

A
Ali

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

Collections don't have a skip method.
S
Slmmgdd

Maybe this

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


S
Suraj Rao
$products = Article::orderBy('id', 'asc')->limit($limit)->get();

B
Brane

Try this sample code:

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

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

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

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

$chunk->all();

N
Nisam

Please try like this,

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

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 )
S
Sadee

Laravel 5.1< (current v 9)

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

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