This episode is for members only

Sign up to access "Build a File Marketplace with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
22. Adding a string macro to properly display the user's name

Episodes

0%
Your progress
  • Total: 3h 32m
  • Played: 0m
  • Remaining: 3h 32m
Join or sign in to track your progress

Transcript

00:00
As a nice little touch, let's go ahead and correctly display the user's name with the apostrophe and S if their name ends with an S. If you're not familiar with this, let's go over and just modify my name to Alex, just so it's a little bit simpler. You can see we've got Alex's Marketplace. Now, typically within English, if we have a name that ends with an S, that's not correct to say James's Marketplace.
00:25
So what we want is a global string helper, which we can use in any of our other projects, which shows this properly. Now, the reason that we're going to create a global string helper via a macro is doing all the logic to display this apostrophe and S within our views is not a good idea, nor is it really a good idea to place this within the model itself because we might need to use it elsewhere. So what we're going to do is head over to our app service provider or any other service provider.
00:51
And under boot, we're going to use the string helper in here to create a macro. Now, if you're not familiar with macros, we just open up the string helper. You can see that it uses this macroable trait. That means we can add any of our own methods onto anything within Laravel that uses that macroable trait.
01:12
So I'm going to go ahead and create a possessive method inside of this string helper. Now, just to show you how this works, if I just return works in here, what we can now do is over in here and let's just do this above here, we can say string possessive and that will output works. So now we can just use this to take in the user's name and show it with an apostrophe S correctly.
01:44
So when we pass values into here, we will actually get the string into this value. So let's go ahead and just pass in Alex into here first of all. Now, what's going to happen here is if the string that we've passed in ends with an S, we want to return just an apostrophe. So we can use the string helper as well, despite the fact that we are inside of a string macro and we can say ends with.
02:11
And we want to check if this ends with a lowercase or an uppercase S so we can use both of them. If that's the case, we just want to return to this a apostrophe so we can double quote that or single quote it and escape it here. It doesn't really matter. Now, we, of course, want to return the string before that. That's going to be James and then just an apostrophe.
02:36
And then otherwise, we want to return the string and then we want to append on an apostrophe and S. So let's go ahead and just escape that and we should be good. So if it ends with an S, just return an apostrophe. Otherwise, return apostrophe and S. So let's try this out. Let's go over and we see Alex's.
02:57
Let's switch this over to James. And we see James just with an apostrophe on the end. So now we can take what we've got here and apply it to the user's name. So let's grab this string possessive. Let's go ahead and replace that out in here.
03:15
Pass the user's actual name in. So user name and we can now get rid of this inside of our template. So if we come over and give this a refresh, there we go. Now, I'm going to go back and update my name to Alex, save that out and there we go.
34 episodes3 hrs 32 mins

Overview

Build a marketplace where sellers can list and sell files, while we take a cut of each sale using Stripe Connect.

We'll cover onboarding users with Stripe Connect, creating products and uploading files, payments, and delivering purchased files to your customers.

Here's everything we'll cover:

  • The Stripe Connect onboarding flow
  • Effortlessly creating products (and uploading files) with Livewire forms
  • Subdomains for your user's marketplace
  • Stripe Checkout for a beautiful, secure payment flow
  • Securely delivering files with Signed URLs in Laravel
  • Showing sales stats on a dashboard
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!