0

How can I add paginate to this query? The pagination data is not showing in my json response.

  $posts = Post::with('comments','settings')
    ->whereIn('user_id', function($query)
        use($id){
            $query->select('followable_id')
            ->from('followables')
            ->where('user_id', $id)->paginate(10);
        }
    )->orWhere('user_id', $id)
        ->latest()
        ->get();

    return response()->json($posts);

2 Answers 2

1

->paginate(10);

The paginate should be on the outside of the query (not within the whereIn closure)

Try to swap ->get(); with ->paginate(10);

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

Comments

1

A quick check of the docs shows:

You may call paginate after setting other constraints on the query, such as where clauses:

Note it says after other constraints - it doesn't make sense to use pagination inside a whereIn() clause. So in your case, that would be something like:

$posts = Post::with('comments','settings')
    // ... your code, no pagination ...
    )->orWhere('user_id', $id)
        ->latest()
        ->paginate(10);

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.