0

I got this piece of code in Laravel:

public function index()
{
    $user_id = auth()->user()->id; // gets the current user id
    $user = User::find($user_id); // find the specific user id

    $validPosts = $user->posts->paginate(5)->whereIn('status', ['Pending', 'Processing']);
    return view('home', compact('validPosts'));
}

I want to return all posts that have the status pending and processing with pagination that has the value of 5 but I am getting the following error:

Method Illuminate\Database\Eloquent\Collection::paginate does not exist.

I know that I have failed somewhere at $user->posts->paginate(5).

What should I do?

5
  • 1
    Call the the posts() method in this way: $user->posts()->paginate(5), when you call $user->posts you receive a collection and you dont have method paginate() there Commented Dec 6, 2018 at 20:32
  • I did that and now I am getting another error when doing this to the view: "{{ $validPosts->links() }}" Method Illuminate\Database\Eloquent\Collection::links does not exist. Commented Dec 6, 2018 at 20:44
  • @AndreiIonut That is because $validPosts is an array. You probably want to loop through the array to get the links. Commented Dec 6, 2018 at 20:53
  • Remove compact method, that is odd. Use all advantages of Eloquent. laravel.com/docs/5.7/pagination#paginating-eloquent-results Commented Dec 6, 2018 at 20:59
  • try this like the answer down here.. $user->posts()->whereIn('status', ['Pending', 'Processing'])->paginate(5) Commented Dec 6, 2018 at 21:19

2 Answers 2

4

Paginate method works with Eloquent models. Retrieve the model, apply whereIn first and then paginate it. Try this:

public function index() {
    $user = auth()->user();
    $validPosts = $user->posts()->whereIn('status', ['Pending', 'Processing'])->paginate(5);

    return view('home', $validPosts); 
}

Now in your "home" view file you can use $validPosts

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this way

public function index()
{
    $user_id = auth()->user()->id; // gets the current user id
    $user = User::find($user_id); // find the specific user id


    // paginate function should be last
    $validPosts = $user->posts()->whereIn('status', ['Pending', 'Processing'])->paginate(5);


    // if you want to pass more variable, do it like this
    return view('home', ['validPosts' => $validPosts]);
}

or you can do it like this

public function index()
{
    $user_id = auth()->user()->id; // gets the current user id 


    // paginate function should be last
    $validPosts = Post::where('user_id', $user_id)->whereIn('status', ['Pending', 'Processing'])->paginate(5);


    // if you want to pass more variable, do it like this
    return view('home', ['validPosts' => $validPosts]);
}

Hope this help you.

1 Comment

I am glad that I help you :)

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.