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
02. App setup with a user domain column

Transcript

00:00
So to start things off we're going to go ahead and get a fresh Laravel app installed. We're going to install Laravel Breeze as a starter kit because this gives us a nice UI where we can update our profile information which will include our subdomain and then we're going to go ahead and add in the subdomain column and get that added to the UI. So first things first
00:21
let's go ahead and create out a new Laravel project and let's just call this Laravel User Subdomains like so. Okay so that's pulled in and we can go ahead and start to configure our database. So I'm going to go ahead and switch the database connection over. You can use any database connection you want at all. You're going to want to go ahead and copy the database name
00:43
or come up with a new one and then go ahead and create that out. You can see I've already created this out in here and once we've switched that over we can go ahead and migrate the default migrations that come with Laravel which will just give us this users table and a few other things as well. So this is where we're going to be adding that subdomain but before we do that let's go
01:04
ahead and get Laravel Breeze installed so we have a nice UI to work with. So we're going to go and pull in Laravel Breeze. We're going to go down to the Breeze and Blade section. Doesn't matter if you're using Inertia or anything else here it will work pretty much in the same way with a few tweaks and we're going to go ahead and run npm install and npm run dev. So let's do npm install and npm run
01:29
dev and we should be good to open this in our browser. Okay yeah so first of all this is asking us which stack we want we're just going to choose blade here and we will not use dark mode here and let's go ahead and say no to pest for test just for now. Okay so I think that has done an npm install and npm run dev but let's just go ahead and do that anyway. Okay great so now we can go ahead and
01:54
access the app url just here laravellusersubdomains.test. This really depends on your local environment so depending on how you are running this you might not have the ability to actually pass subdomains in here. You can see that I can just do this and this works by default Laravel is set up to work with wildcard subdomains. You'll have to do a little bit of research depending on
02:18
your local environment and how you're running Laravel just so you have the ability to specify a subdomain. Okay so now that we've got this up and running let's go ahead and just register an account in here. So we're not going to allow a subdomain to be chosen at this point but we are going to allow one to be chosen in this profile area. So before we get to that let's go and create a
02:44
migration out to add the subdomain to the users table. So let's create a migration let's head over to that add subdomain to users table and let's come down and fill this in. So this is going to be a string we're going to call this subdomain and really importantly we want this to be unique and we also want this to be nullable because when we first register this is just going to be set to
03:10
a null value. So let's fill in the down migration here as well so table drop column and of course we're going to drop that subdomain column and we are ready to migrate so php artisan migrate. Okay so now we have over in our database table a new subdomain column in the next episode we're going to go ahead and add this to the ui so the user can update it.
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.