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
03. Validating and updating the subdomain

Transcript

00:00
let's allow our users to choose a subdomain from this profile information section just here to find this we're going to go over to our profile controller which is under app http and controllers this is installed when we install breeze and you can see here to edit a profile we go through to this profile edit view so let's open that up that's under edit.blade.php under resources
00:24
views and profile and down here we have this update profile information form partial which lives in this directory just here so that contains the email address and everything else that we see in here and of course we're just going to go ahead and add in a new part of this form so let's do this under the email address and we can pretty much just copy everything for email here and put
00:48
this down underneath here and we should see a new field great so let's swap this over this is going to be subdomain this is going to be subdomain as well and so will this and this as well sub domain now we also want to get rid of the auto focus for this we want to get rid of the auto complete for this as well and we also want to get rid of required because it's not a required field
01:16
and we want to switch over the old function to pull through the subdomain if we have any validation errors and we want to switch the validation error over as well so we now have the ability to pass in a subdomain and of course this isn't right so this is going to be user and subdomain as well and there we go so the question is for this subdomain how do we validate this and
01:36
more importantly insert it into the database well if we head back over to our profile controller here you can see that when we update this we have a form request in here if we go ahead and open this up this lives in the requests section under http this contains all of the validation rules that we need to store so really what we want to do is just add in a new validation rule to this list
02:02
and then choose all of the validation rules that are appropriate for this subdomain so for this really importantly we want this to be a string so we can just add that in there but we also want this to be validated as alpha so this can only contain letters that's it we don't want any numbers in there we don't want any hyphens we don't want any kind of special characters you could potentially
02:25
include numbers if you wanted to but we'll keep it really simple for now we also want to make sure that this is unique now the database level we already have the unique rule in there so it won't be allowed but it makes sense to show this error to the user so we're pretty much going to copy the unique rule for the email address which has pretty much the same functionality to it and we want to
02:48
make sure that this is unique to the user but we also want to ignore if the user already has this subdomain now that's really important because if i choose a subdomain like alex and then i choose to save this again we don't want this to error to say that it's already being used because that's being used for me so this just applies to other records that don't include my user okay so now
03:15
that we've done that let's go ahead and see if we can update this subdomain with my first name hit save and you can see it hasn't actually stored this so we want to go over to the user model and we want to include this within our fillable fields so let's include sub domain in here and we should be good so i'm going to once again choose alex as the subdomain and there we go that has now been
03:40
saved and my subdomain has been selected if we had another user also try to use the subdomain alex that would then fail because of course it's unique we also have the ability to validate against this because it only needs to contain letters and so on so now we can go into our profile information choose our subdomain and we now know that's going to be unique for every single user
9 episodes 31 mins

Overview

Allow users to choose their own subdomain, and give them their own area of your application where only their details are shown. You'll be able to register multiple routes for user areas.

Everything in this short course can be easily applied to your own applications where you need this functionality. We'll cover every step of the way, and some things to watch out for.

Finally, we'll learn how to deploy this and configure nginx and DNS records so you're ready to go.

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

Comments

No comments, yet. Be the first to leave a comment.