This episode is for members only

Sign up to access "Custom User Subdomains in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
08. Nullable subdomains

Transcript

00:00
There's a slight issue with the way that we set up our validation rules earlier. And that is, if we wanted to get rid of the subdomain and not have one anymore, when we save this out,
00:08
you can see that the subdomain must be a string and the subdomain must only contain letters. That's not great. So we're going to head back over to the profile controller,
00:18
open back up the profile update request, and then come down and we're going to make this nullable. What that will allow us to do is go ahead and ignore all of these other rules if this thing
00:30
that we have submitted and attempted to store and validate is null. So now I can go ahead and get rid of the subdomain, save that out, and I now no longer have a subdomain.
00:41
If we try and access alex.laravel, use the subdomains.test, sure enough, we get a 404. Let's bring this back because I want to go ahead and save my subdomain, and we have fixed that up.

Episode summary

In this episode, we take a closer look at how we handle subdomains in our application, especially when it comes to validation. At first, our validation rules make it impossible to remove a subdomain—if you try to clear it out, it complains that the subdomain must be a string of letters. Not ideal if a user wants to have no subdomain at all!

To fix this, we jump into the profile update request and make the subdomain field nullable. This change tells Laravel to ignore the other validation rules when the field is left empty (i.e., null). After making this update, we can now remove the subdomain entirely, save the profile, and the system behaves just as expected—if you try to access the old subdomain URL, you’ll get a 404.

Finally, we quickly demonstrate bringing back the subdomain and saving it again, showing that everything works smoothly in both cases. This update makes the user experience a lot more flexible when managing subdomains!

Episode discussion

No comments, yet. Be the first!