Laravel Eloquent 限制和偏移 [英] Laravel Eloquent limit and offset

查看:30
本文介绍了Laravel Eloquent 限制和偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的

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

请帮我一把!

谢谢!

推荐答案

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

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

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

跳过/拍摄

限制查询返回的结果数量,或者跳过一个给定查询中的结果数量(OFFSET),您可以使用跳过并采取方法:

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

laravel 5.3 中,您可以编写 (https://laravel.com/docs/5.3/queries#ordering-grouping-limit-and-offset)

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

这篇关于Laravel Eloquent 限制和偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆