0

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 Answers 2

1

You made collection to array and trying to use simplePaginage method which is for collection not array.

Use simplePaginate inside model method before converting to array.

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Now it working perfectly, but links() or render is mot working in my view , kindly give suggestion in this case.
@user7597883 : You can make another question with fully detail as here no detail contain in your current question regarding view's problem.
it's again give error that simplePaginate does not exist
0

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

1 Comment

I have to return an array because of my logic in views depends on array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.