laravel8 数据结构问题

class UserController extends Controller
{
    /**
     * 显示该应用程序的所有用户。
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::table('users')->paginate(15);

        return view('user.index', ['users' => $users]);
    }
}

这里的$users是对象还是集合还是数组呢?
laravel8 数据结构问题

这个是DD出来的结果,看不明白具体数据结构

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
Mutoulee
最佳答案
Illuminate\Pagination\LengthAwarePaginator {#1424 ▼
  #items: Illuminate\Support\Collection {#1417 ▼
    #items: array:5 [▼
      0 => {#1404 ▶}
      1 => {#1409 ▶}
      2 => {#1405 ▶}
      3 => {#1408 ▶}
      4 => {#1410 ▶}
    ]
    #escapeWhenCastingToString: false
  }
  #perPage: 15
  #currentPage: 1
  #path: "http://xxx.test"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
  #total: 5
  #lastPage: 1
}

自己dd出来看吧,首先它不是集合也不是数组,而是一个分页对象,即:Illuminate\Pagination\LengthAwarePaginator,对象中包括了数据列表、分页相关参数等等。

1年前 评论
讨论数量: 5
翟宇鑫

dd 一下不就知道了吗。这种问题为什么会问出来。

1年前 评论
Richard852555 (楼主) 1年前

paginator的一个对象吧,函数看看下一层就知道返回了。

1年前 评论
Mutoulee
Illuminate\Pagination\LengthAwarePaginator {#1424 ▼
  #items: Illuminate\Support\Collection {#1417 ▼
    #items: array:5 [▼
      0 => {#1404 ▶}
      1 => {#1409 ▶}
      2 => {#1405 ▶}
      3 => {#1408 ▶}
      4 => {#1410 ▶}
    ]
    #escapeWhenCastingToString: false
  }
  #perPage: 15
  #currentPage: 1
  #path: "http://xxx.test"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
  #total: 5
  #lastPage: 1
}

自己dd出来看吧,首先它不是集合也不是数组,而是一个分页对象,即:Illuminate\Pagination\LengthAwarePaginator,对象中包括了数据列表、分页相关参数等等。

1年前 评论
dd($users); // 对象
dd($users->toArray()); // 数组
dd(collect($users->toArray())); //集合

laravel 中的集合其实就是 Illuminate\Support\Collection 对象

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!