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
05. Route model binding to resolve the user

Transcript

00:00
Clearly the way that we're doing this at the moment is not going to work very well because we're going to have to pick the user out of the database every single time we register a
00:08
route here. Now let's go ahead and just get rid of what we've done here and the first thing I'm going to do is create out a controller in here called home controller which represents a controller under the subdomain category of controllers. This isn't part of our actual app, it's part of the user within our app. So let's comment this out, go ahead and create out a new controller for this.
00:31
So let's make a controller. I'm going to put this in a folder called subdomain just so I know which ones are part of the subdomain section of our app to separate them out. So let's pull this back in and pull the namespace in for this and we can head over to the home controller now and just create this out. So we'll probably get a request in here if we need it and now we want to figure out how
00:56
to conveniently grab the user within here. So this should work if we just come back over and give that a refresh. Any domain that we access now is going to hit that controller. Now what we could do is we could use root model binding within the root domain definition. What that will do is it will grab a user by their id and then we can just have them injected within this controller. So let's go
01:20
ahead and just die dump on that user and see what we get when we pass a id of a user in. So if we head over to the database this is id 1. So really what we could do now is say 1.laravel user subdomains.test and sure enough we get my user. If we had another user with an id of 2 that's going to work in exactly the same way. Now that's not what we want. We want to pluck them out by their
01:45
subdomain. So with root model binding what we can actually do is choose the column that we want to identify this model by. So if I just use colon and then subdomain what this is now going to allow me to do is pass in the subdomain of the user and then that's going to be looked up by their subdomain. We have root model binding now that's automatically looked that user up for us and it's available in
02:10
our controller method here. So we can just start to output stuff about this user and this user only. So now all we need to do is start to define out any other routes we want about that particular user and that's just going to work and in every controller we define here we can just do exactly what we've done here and define the user in here that gets plucked automatically from the database.

Episode summary

In this episode, we're cleaning up the way we fetch users based on the subdomain they visit. Previously, we were manually grabbing the user from the database in each route — definitely not ideal.

So, we kick things off by setting up a dedicated HomeController for our subdomain routes, keeping our code organized and tidy. After creating the controller (and putting it in its own subdomain folder), we talk about a much more convenient way to pull in the user: route model binding.

Laravel's route model binding allows us to directly inject a user model into our controller based on a specific route parameter. But, instead of the default (fetching by id), we tweak it to use the user's subdomain column. That way, when someone visits something like alex.laravel-user-subdomains.test, the corresponding user gets automatically fetched from the database. It makes everything smoother and means we don't have to do the manual database lookups anymore.

From here, any new routes or controllers that need the user can just use the same pattern, and Laravel will handle pulling in the right user for us. It’s much cleaner and way more scalable!

Episode discussion

No comments, yet. Be the first!