laravel-paginate

Laravel sortBy paginate

余生颓废 提交于 2019-12-01 05:40:02
I have a posts table and comments table, comment belongs to post, and I have the relationship setup in Post and Comment model. I did sort posts by the number of comments of each post like this: $posts = Post::with('comments')->get()->sortBy(function($post) { return $post->comments->count(); }); What I wonder is how I can paginate these sorted posts? $posts = Post::with('comments')->get()->sortBy(function($post) { return $post->comments->count(); })->paginate(20); doesn't work and gives me error that says paginate is an undefined method. I don't know if you can do it using Eloquent but you can