In this episode, we focus on making the dates and times in our chat interface a lot more readable and user-friendly. Instead of always showing the full date and time for messages, we upgrade things so that messages from today display "today", those from the previous day display "yesterday", and anything older falls back to a normal date string.
We kick things off by creating a new accessor attribute on the message model, which is responsible for returning our improved, human-readable date string. First, we use a simple set of if statements to check whether a message was created today or yesterday using Carbon's handy methods. Depending on the result, we set the label accordingly and append just the hours and minutes (no one needs those seconds!). Otherwise, we fall back to displaying the plain old date.
After confirming that's working, we realize that piling on more and more if statements could get messy. So, we refactor the code to use PHP's new match
statement to keep things concise and scalable. This way, it's super easy to add more conditions down the line and our code stays clean and readable.
By the end of the episode, we've got a much better way to display message times in our chat app, making the whole experience a bit nicer for users.