dongwen9051 2017-03-14 06:21
浏览 361

在数组上调用laravel的paginate函数?

Here is my controller function where i am paginating my results

 public function showallUsers() {

    $user = User::getUsers()->simplePaginate(5);
    return view('sellerTable', compact('user'));
}

I am using Eloquent queries in my models to get data from database , here is my getUser method in the User model.

 protected static function getUsers() {
    $data = User::where('role', '=', 'seller')
                    ->where('archive', '=', 0)->get(['id', 'user_name', 'first_name', 'last_name', 'phone_no', 'gender', 'facebook_id', 'gmail_id', 'email', 'braintree_customer_id', 'role']);
    return (!empty($data)) ? $data->toArray() : array();
}

When i use to hit my controller function the following error comes

   Call to a member function simplePaginate() on array

It is comming because i am returning an array in Model User to UserController . Laravel is not allowing me to call a simplePaginate Function on an array , any ideas that how can i call laravel Paginator on an array ?

  • 写回答

2条回答 默认 最新

  • douya2433 2017-03-14 06:27
    关注

    Why you need to return an array instead of collections.

    Change your function like this

    protected static function getUsers() {
        $data = User::where('role', '=', 'seller')
                        ->where('archive', '=', 0)->get(['id', 'user_name', 'first_name', 'last_name', 'phone_no', 'gender', 'facebook_id', 'gmail_id', 'email', 'braintree_customer_id', 'role']);
        return $data;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题