This episode is for members only

Sign up to access "Build a Forum with Inertia and Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
19. Ordering discussions by last post

Episodes

0%
Your progress
  • Total: 6h 54m
  • Played: 0m
  • Remaining: 6h 54m
Join or sign in to track your progress
01. Introduction and demo
6m 59s
0%
02. Getting set up
10m 36s
0%
03. Modifying registration for usernames
7m 15s
0%
04. Figuring out the forum layout
5m 57s
0%
05. Creating and listing topics
9m 15s
0%
06. Basic discussion listing
13m 33s
0%
07. Pinning discussions
4m 1s
0%
08. Tackling pagination in Inertia
8m 23s
0%
09. Customising pagination text in Laravel
52s
0%
10. Showing a discussion
6m 4s
0%
11. Setting up discussion posts
5m 53s
0%
12. Listing through discussion posts
5m 28s
0%
13. Adding more data to posts
8m 24s
0%
14. Adding pagination to posts
1m 35s
0%
15. Adding a post preview to discussions
4m 52s
0%
16. Adding the last reply to discussions
5m 54s
0%
17. Outputting discussion participants
8m 4s
0%
18. Limiting participants in the UI
5m 56s
0%
19. Ordering discussions by last post
4m 30s
0%
20. Handling deleted users
2m 31s
0%
21. Counting replies
8m 13s
0%
22. Building our first filter
8m 31s
0%
23. Highlighting current filters, and merging with pagination
5m 37s
0%
24. Adding auth specific filters
6m 40s
0%
25. Adding the topic filter
8m 18s
0%
26. Scaffolding the new discussion form
13m 29s
0%
27. Toggling the create discussion form
9m 2s
0%
28. Keeping form state
4m 59s
0%
29. Storing a new discussion
11m 29s
0%
30. Discussion validation and authorization
5m 1s
0%
31. Generating markdown for posts
8m 37s
0%
32. Toggling the markdown preview
7m 43s
0%
33. Fetching and displaying markdown
8m 6s
0%
34. Adding a markdown shortcut toolbar
5m 53s
0%
35. Dealing with SVG icons
7m 46s
0%
36. Creating the reply form
7m 48s
0%
37. Basic Inertia permission checking
6m 26s
0%
38. Creating replies to discussions
5m 37s
0%
39. Jumping to posts
11m 40s
0%
40. Automatically scrolling to posts
6m 18s
0%
41. Toggling post editing
7m 32s
0%
42. Editing posts
3m 36s
0%
43. Deleting posts
4m 21s
0%
44. Deleting discussions
6m 7s
0%
45. Setting up for best answers
7m 29s
0%
46. Toggling the best discussion answer
12m 14s
0%
47. Solved and unsolved filters
2m 23s
0%
48. Indexing discussions for search
8m 6s
0%
49. Searching discussions
12m 35s
0%
50. Debouncing search
2m 47s
0%
51. Adding mentionable functionality to forms
6m 32s
0%
52. Indexing users for mentioning
9m 9s
0%
53. Hooking up users for mentions
10m 10s
0%
54. Detecting and storing mentioned users
9m 54s
0%
55. Adding the mentioned filter
2m 26s
0%
56. Adding mentions to the markdown toolbar
1m 1s
0%
57. Adding mentions to the reply form
7m 21s
0%
58. Fixing up some unauthenticated state
1m 1s
0%
59. Fixing up post scrolling
1m 48s
0%
60. Reviewing SSR (Server-side rendering)
8m 20s
0%
61. Preventing parent posts from being deleted
2m 31s
0%
62. Improving solution marking
4m 9s
0%

Transcript

00:00
Now this is a really tricky one. How do we order discussions by the latest post? That means which discussions have had the most recent reply.
00:11
Well to settle this up we're first of all going to remove the pinned from this top discussion here and give that a refresh. Now we know that this one was posted first and this one was posted last. So this one is naturally at the top but this one has had a post most recently. So we want this one to bump to the top so the relevant more replied to discussions are at the
00:36
top. So we're going to go ahead and create a scope for this and then we'll just fill it in. So over in the discussion model where we have our other scope which is here ordering by pinned, we're going to create another scope in here order by last post. So let's bring our query into here and we're not going to do anything with this just yet but over on here we are going to order this
01:06
by last post and let's go over and order by last post and there we go. So we get no difference whatsoever. So what we really need to do inside of this scope if we think about it is grab the latest post created at timestamp and then order the discussions by that timestamp because the created at timestamp will determine the latest thing that needs to display. So if we have a
01:40
create at five minutes ago and we have a create at 10 minutes ago the five minutes ago one is going to be the very top. So how do we do this? Well let's go ahead and say query order by so we just use an order by as we normally would. This here needs to be the created at date of the last post and then we need to order that in a specific order. In our case it's going to be descending
02:02
because the latest needs to be at the top. So in here what we can do if we just pull this down so it's a little bit easier to work with is almost create like a sub query in here to pick out the created at date for the latest post. So we're going to say post select so we're going to use selected because we only need the created at column and then in here we're going to say where
02:26
column and from posts we know that the relationship here is the discussion id and that needs to match up to the discussions id. So basically we're just matching that up with a join. Then we're going to say latest so we want to get the latest post at the top or the latest posts at the top and then we want to take one. So what we're doing here is grabbing the latest post
02:51
from that discussion with this join and then we're only returning the created at and if we pass that into order by that's going to order it by this date of the latest post. So if we come over now and just give this a refresh we don't see any difference yet and I mentioned that a little bit earlier on in the course that is because we have latest here which we added a comment to remove
03:13
when we've implemented the ordering by the last post. So I'm going to get rid of that now and now we do not see that jump to the top because of the manual insertion of the post here. This is 13 minutes from now into the future so of course this one's going to be latest. So third time lucky we're going to go over to our posts and for let's just have a look here. Here is a post let's set that to
03:41
an hour earlier. It's just because I manually entered it and maybe let's bump that down even further just in case. So give that a refresh that bumps to the top. Now let's add manually now with the correct time a post to this and see this one jump to the top. So this is need help with view we can copy this down and let's set that to now which is technically in the future but
04:08
the concept remains the same and that's going to have a parent id of three and the discussion and the user can be the same and because we've technically now replied even though we haven't to this one that should now bump to the top. So there we go now all of our discussions are ordered by the latest post or latest reply.
62 episodes6 hrs 54 mins

Overview

Ready to build a forum with Inertia and Laravel?

Why a forum? A forum touches a whole load of concepts that you'll use throughout your development career – particularly on the client-side, where we'll be doing most of the heavy lifting.

So, let's build a clean, modern forum with features like markdown support, code highlighting, advanced filtering, user mentions, full-text search, the ability to mark best answers, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!