Hoda Kh
Hoda Kh

Reputation: 355

How to get posts of a taxonomy in descending order Corcel Laravel

I'm using Corcel in my Laravel project and I have set my database connection to a wordpress database. I want to get the posts of a taxonomy in descending order according to post_date field. I've developed the following function but it returns the posts in ascending order. How should I change it?

public function posts(Taxonomy $category)
{

       return PostResource::collection($category->posts);

}

Upvotes: 0

Views: 914

Answers (1)

Farouk M
Farouk M

Reputation: 1295

There is multiple available methods on the Laravel Collection : https://laravel.com/docs/5.7/collections#available-methods

Try this one :

return PostResource::collection($category->posts)->sortByDesc('post_date');

This method has the same signature as the sortBy method, but will sort the collection in the opposite order.

Upvotes: 1

Related Questions