简体   繁体   English

在数组上调用laravel的分页功能?

[英]calling paginate function of laravel on a array?

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. 我在模型中使用口才查询从数据库中获取数据,这是用户模型中的getUser方法。

 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 . 之所以这样,是因为我将Model User中的数组返回给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 ? Laravel不允许我在数组上调用simplePaginate函数,关于如何在数组上调用laravel Paginator的任何想法?

You made collection to array and trying to use simplePaginage method which is for collection not array. 您将集合制作为数组,并尝试使用simplePaginage方法,该方法用于集合而不是数组。

Use simplePaginate inside model method before converting to array. 在转换为数组之前,请在模型方法内使用simplePaginate

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']);
    $data = $data->simplePaginate(5);
    return (!empty($data)) ? $data->toArray() : array();
}

and use in controller like this 并在这样的控制器中使用

$user = User::getUsers();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM