Playing
01. Laravel Route Model Binding

Episodes

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

Transcript

00:00
Root model binding within Laravel is a great way to speed up your development, make your code just that little bit cleaner and this works if you are
00:10
pulling in a resource for example a user based on a ID that you pass into the URI. So we're going to look at the way that we would traditionally do this and then we'll hop over to our root service provider and look at how we can bind models to roots. So for example if we had a user's URI and we wanted to pull a
00:32
user in by their ID. We know that we can do this by specifying the ID and I've already set up my database with a user and I can just pull in the model like this. If we just do a quick die dump on user here and just hop over to the browser you can see that if we go and say user one for example we get this
00:56
user model back. So like I said I have a couple of users set up in here, two just in here and to actually pull this user in all I do is I'd say user equals user and I could say find or fail for example and I could just pass in that ID and we could just do a die dump on user name and for one that would give Alex, for two
01:19
that would give Dale and anything that doesn't exist we would get a not found HTTP exception, a model not found exception. So this is fine like the code isn't too bad once again you can do this within a controller but there's a much easier way that we can do this and that is using root model binding. So if you go
01:40
over to app providers and your root service provider under boot we can register root model bindings here and we have this router just available to us here so we can say router model we can choose the name so the key name in this case it will just be user and then in here we can give the name of the model
02:00
so we can say app user class or you can add that in as a string. So let's just pull this down here and we're good to go. So how does this change things? Well let's just switch this over to user and let's just get rid of all of this here and let's just do a die dump on user and see what happens. We can see here that
02:23
we've now got a user model but this actually contains the user with the ID of one so it's automatically looked this up by the primary key and it's returned to us that user. So all we need to do now is say die dump user name for example and that gives us that user's name again with two it works in exactly the
02:45
same way and this works for other models as well so for example if you wanted to get the users topics you could pass in a topic here and you could go ahead and pull in topic so for example if you wanted to get topic ID of one for a particular user you can go ahead and pass this in as well and of course you
03:05
can just go ahead and bind this in again so you could say topic and if you did have a topic model I don't have one set up but you could do that very similarly. Now I don't really like this approach too much because if you have your relation set up properly what you should be able to do is say user topic and then
03:24
you can go ahead and use a repository if you have a slightly larger application or to be honest you could just do this. I prefer this way but of course you can bind two if you want and make your code even shorter and even easier. So that is root model binding within Laravel and how we can use it to speed things up.
1 episode 3 mins

Overview

Tidy up your code drastically by binding models to routes and automatically fetching a model by it's primary key when you access a URI like /users/1.

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

Episode discussion

No comments, yet. Be the first!