This episode is for members only

Sign up to access "Mastering Polymorphic Relationships" right now.

Get started
Already a member? Sign in to continue
Playing
04. Custom Polymorphic Types

Transcript

00:00
Before we go any further we're going to talk about custom polymorphic types and creating out our own morph map. Now that sounds more complicated than it is.
00:09
The first thing that we're going to do is head over to our database and we're going to look at our imageable type which we already know hooks this image up to any of these models so we just have one table. Now this is great because obviously we need to know where these models actually live to be able to retrieve the relationship once we've stored them in the
00:30
database. But what happens if the location of your model changes around? Well let's just demonstrate that first of all over in the browser and then we'll go ahead and look at how we can make this better with custom polymorphic types. So let's just grab our user here let's say user find one that we did before and let's die dump on our user image and we should get back that abc that we
00:55
stored in the last episode. Great. Okay so what I'm going to do is over in the project here I'm going to go ahead and move this around so let's say that we have a separate directory in here specifically for users and I went ahead and moved that. In my IDE I can have this update all of my code so it puts it in the right location it changes the namespace changes any of the imports that I
01:17
have anywhere in my application so this will still be able to find this user. So if we say user here you can see we still get the user it still works but when we try and find the image that's associated with that user we get null. Now that's pretty obvious because now the imageable type is different and it can't be looked up which is not what we want. So what we want to do ideally is
01:39
figure out a way to go ahead and make this not dependent on the location of the model that's really important. So how do we do this? Well I'm going to go ahead and write the code within a service provider to change this over and then I'm just going to manually change this over in the database and we'll finish up by just looking when this gets inserted it enforces what we've given it.
02:02
So we've got two models that could potentially be hooked up here let's go over to our app service provider or any other service provider that we want to use and let's go down to the boot method. We're going to go ahead and pull in the relation class from database eloquent relations and we're going to say enforce morph map now what this allows us to do is map up
02:23
the string of our choice to the model location. So in our case let's start with user we are giving this the name user this is the key that's going to get stored in the database. What we're then going to do is we're either going to provide manually the full namespace to the user model which in our case now lives within a user directory or which I always prefer to do because
02:45
my IDE will change this over for me if I ever move the location of it we're going to say user class like so. Now we're going to do that for both of our models so we're going to say post and post. Now before we take a look at this a really important part about this is probably do this first thing when you first set your project up. Obviously it's pretty straightforward to go in
03:05
and query and change these over later if you ever decide to but when if you are working with polymorphic relationships I'd always recommend setting up a morph map do it as soon as you start your project and you start to introduce polymorphic relationships. Okay now that we've done this let's go over there won't be any difference in the browser yet because this
03:25
should be user now that we've given it a key and this should be post. Now that looks a lot tidier regardless now of where that model moves to in our application it's still going to work. Let's go over to the browser give that a refresh and you can see that's now using our morph map that we've defined to pluck this out of the database. Okay let's finish up by just creating out a
03:47
basically storing a record and we'll see how this works. So let's say image and we can either say new image or image make whatever you want to do. Let's go ahead and give a url in here so let's say df and let's say user image and save and we're going to save that to the database let's go over and yeah of course I actually need to pass the image in makes sense there we go and now you
04:14
can see that that has respected our morph map and it's filled this in so regardless of how I change this around in my application for example I'm going to move this back into the models directory my IDE's changed everything around for me I'm going to delete that come over and give that a refresh and that's still going to go ahead and work in exactly the same way and of course it's
04:33
still going to work in exactly the same way if I go ahead and access this image so didump user and image and let's get rid of that make and there we go perfect so now a lot more flexible a lot easier to just have things work if your location of your models ever move. It's the first thing I'd recommend you do when you start working with polymorphic relationships.

Episode summary

In this episode, we dive into the world of custom polymorphic types in Laravel, specifically focusing on how to create and use your own morph map. It might sound complicated, but we'll break it down step by step.

We start off by looking at a common pain point: when you move your models around in your application, the default way Laravel stores the type for a morph relationship (the fully qualified class name) can break your relationships if those namespaces change. We'll see firsthand what happens when you try to access a related image after moving a model to a different directory—spoiler, you get null!

To fix this and make your app more flexible, we introduce morph maps. We'll go to our service provider and set up a custom map that links simple strings (like 'user' and 'post') to the correct model classes. We talk about why it's best to do this up front as soon as you start using polymorphic relationships, and show how to update both your code and your database to respect these friendly type keys.

Finally, we demo how saving and retrieving polymorphic data now uses the new morph map, making your relationships robust no matter how much you refactor or move things around in your codebase. The episode wraps up with a strong recommendation: always use morph maps for polymorphic relationships from the start to save yourself future headaches!

Episode discussion

No comments, yet. Be the first!