dfinchenko's avatar

How can I get part of posts ordered by updated_at and other part by created_at with pagination

Hi! How can I get part of the posts ordered by updated_at and another part by created_at with pagination? For example: have 20 posts and I need to show on page 10 posts where 5 posts order by created_at and 5 posts order by updated_at.

0 likes
19 replies
vincent15000's avatar

That's not possible.

That's like if you want to have a list of numbers ordered at the same time ascending and descending.

If you need to use both ordering criteria, you have to retrieve the 10 posts you need to order.

Then you have to apply the created_at order to 5 and the updated_at order to 5.

Then merge both results and send it to the view.

1 like
dfinchenko's avatar

@vincent15000 Yes, but when I retrieve 10 posts can happen that the latest posts with created_at or updated_at not retrieved.

2 likes
Shivamyadav's avatar

@dfinchenko That means you want to get the latest post ? if you are trying to get the latest post then use

$posts = Post::latest()->paginate(5);
2 likes
dfinchenko's avatar

@Shivamyadav No, I want to get two parts of posts on every pagination page - first part is ordered by created_at and the second ordered by updated_at. Totally for example 10 posts on page which 5 are ordered by created_at and 5 are ordered by updated_at.

Sinnbeck's avatar

@dfinchenko so duplicates are fine? All posts can be in either group on the page it fits on? Otherwise the count might not match

Shivamyadav's avatar

I am not sure but atleast try it..

$posts = Post::orderBy('created_at', 'desc')->orderBy('updated_at, 'desc')->paginate(5);
2 likes
vincent15000's avatar

@Shivamyadav Not sure that it answers his question. I have understood that he has 10 posts and he wants to order them like this.

  • the first 5 posts ordered by created_at

  • the last 5 posts ordered by updated_at

The problem is that these 10 posts are retrieve through a pagination among 20 posts.

1 like
Shivamyadav's avatar

@vincent15000 yeah! I understand the problem .. I am just telling that I am not sure that the code will works as he want or not1

2 likes
dfinchenko's avatar

@vincent15000 I have posts (for example 100 posts in the table) and I need to show these posts (10 per page) using pagination (it can be not only native laravel pagination, maybe using method skip). The First 5 posts are ordered by created_at and the other 5 posts are ordered by updated_at on every page.

1 like
vincent15000's avatar

@dfinchenko I would do something like this.

$postsNotOrdered = Post::paginate(10);

Then you handle the result.

$postsGroups = $postsNotOrdered->split(2);
$postsOrderedByCreatedAt = $postsGroups[0]->sortBy('created_at');
$postsOrderedByUpdatedAt = $postsGroups[1]->sortBy('updated_at');
$posts = $postsOrderedByCreatedAt->merge($postsOrderedByUpdatedAt);
1 like
dfinchenko's avatar

@vincent15000 Yes, cool idea, but it's just ordered only 10 retrieved posts on page. In this case in table can be a post which have more latest created_at or updated_at

1 like
vincent15000's avatar

@dfinchenko I think that you don't explain entirely what you need.

How do you retrieve the latest posts ? Can you share your code please ?

dfinchenko's avatar

@vincent15000

$page = $request->input('page');
$posts = Post::query();
$posts->orderByDesc('created_at');
$posts->orderByDesc('updated_at');

$posts = $posts->skip(10 * ($page - 1))
              ->take(10);
1 like
dfinchenko's avatar

If post have created_at equals updated_at then this post add to part which ordered by created_at

1 like
vincent15000's avatar

@dfinchenko That's totally different than what you already have explained.

If created_at equals updated_at, then this post is ordered by update_at.

If that's your only problem, here is the solution.

$posts = Post::orderByDesc('created_at')->orderByDesc('updated_at')->paginate();
1 like

Please or to participate in this conversation.