0

I want to use pagination after getting my data from api resource But the server responds:

 Method Illuminate\Database\Eloquent\Collection::pagination does not exist.
  public function index(Request $request )
    {
        $perPage=$request->per_page;
        return response()->json(['user'=>UserResource::collection(User::with('roles')->get()->pagination($perPage))],200);
    }

2 Answers 2

2

You don't have to call paginate on get function

Just

public function index(Request $request ) {
    $perPage=$request->per_page;
    return response()->json(['user'=>UserResource::collection(User::with('roles')->pagination($perPage))],200);
}

Even Better

public function index(Request $request ) {
    $perPage=$request->per_page;
    return new UserCollection(User::with('roles')->paginate($perPage));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Call to undefined method Illuminate\Database\Eloquent\Builder::pagination()
0
        return response()->json(['user'=>UserResource::collection(User::paginate($perPage))],200);

this is worked correctly ! 100% ok

Comments

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.