当使用 paginate 方法时,将得到 Illuminate\Pagination\LengthAwarePaginator 实例。当使用 simplePaginate 方法时,则会得到 Illuminate\Pagination\Paginator 实例。这些对象提供几种方法用来描述结果集。除了这些辅助函数之外,分页器的实例也是个迭代器,并且可以像数组一样来使用循环取值。
这时可以依据你的需求决定创建 Illuminate\Pagination\Paginator 或者Illuminate\Pagination\LengthAwarePaginator。Paginator 类不需要知道数据的总条数;然而也正是因为这点,导致它无法提供获取最后一页的方法。LengthAwarePaginator 和Paginator 参数几乎相同;但是它却需要知道数据的总条数。
Eloquent: API 资源简介在构建 API 时,你往往需要一个转换层来联结你的 Eloquent 模型和实际返回给用户的 JSON 响应。比如,你可能希望显示部分用户属性而不是全部,或者你可能希望在模型的 JSON 中包括某些关系。Eloquent 的资源类能够让你以更直观简便的方式将模型和模型集合转化成 JSON。当然...
{ "first": "http://example.com/pagination?page=1", "last": "http://example.com/pagination?page=1", "prev": null, "next": null }, "meta":{ "current_page": 1, "from": 1, "last_page": 1, "path": "http://example.com/pagination", "per_page": 15, "to": 10, "total"...
所以,再来看看http://laravel-api.dev/api/users?include=role的返回数据: {"data":[{"name":"Otha Daugherty I","email":"witting.grayson@example.org","role":{"data":{"name":"nisi","slug":"quia","permissions":null}}},{"name":"Miss Myrtie Stamm","email":"kathlyn.prosacco@example.co...
15 "first": "http://example.com/pagination?page=1", 16 "last": "http://example.com/pagination?page=1", 17 "prev": null, 18 "next": null 19 }, 20 "meta":{ 21 "current_page": 1, 22 "from": 1, 23 "last_page": 1, 24 "path": "http://example.com/pagination", 25 "...
In the example below, a call is made to the Laravel cache system. By glancing at this code, one might assume that the static method get is being called on the Cache class.1$value = Cache::get('key');However, if we look at that Illuminate\Support\Facades\Cache class, you'll see ...
之前在通过 Laravel 创建一个 Vue 单页应用(二)中完成了UsersIndex组件异步地从API中加载用户。 简化了从数据库构建一个真实的后端API,选择通过Laravel的factory()方法在API返回中模拟假数据。 如果你还没有读过通过Laravel构建Vue单页应用的第一部分和第二部分,我建议你先去看看,再回到这里。我会在这里等你。
Eloquent: API 资源 简介 当构建 API 时,你往往需要一个转换层来联结你的 Eloquent 模型和实际返回给用户的 JSON 响应。 Laravel 的资源类能够让你以更直观简便的方式将模型和模型集合转化成 JSON 。 生成资源 你可以使用make:resourceArtisan 命令来生成资源类。默认情况下生成的资源都会被放置在应用程序的app/...
当构建 API 时,你往往需要一个转换层来联结你的 Eloquent 模型和实际返回给用户的 JSON 响应。Laravel 的资源类能够让你以更直观简便的方式将模型和模型集合转化成 JSON 。 生成资源 你可以使用make:resourceArtisan 命令来生成资源类。默认情况下生成的资源都会被放置在...