Playing
03. One to One: Saving

Transcript

00:00
Okay, we're going to use our imagination here and we're going to imagine that both the user and the post have a UI interface that allows us to create out an image or upload an image and then save it to either of the models. Remember here the goal is flexibility. If we're on a user's upload page, we can upload an image and attach it. If we're on a post's upload page, we can create an image and attach it.
00:23
So let's start with the user, then we'll just swap things over to see that we can do this with both of the models. So we've got our user here. Let's imagine that we want to create a new image. So I'm actually going to get rid of both of these from the database. And let's go and first of all create the image. So we'll imagine that this is getting uploaded or something like that. And we don't actually need to store this at this point. We don't need to store this in the database at this point.
00:47
And really we can't do this because we need the imageable type, the imageable ID to match up. So we're going to go ahead and create out a new image model and we're going to fill in just all the metadata that we actually need. So that's just the URL. Set that to ABC like we did earlier. So now that we've done this, what we want to do is we want to take the relationship, which you guessed is image, and we want to go ahead and save it.
01:11
So let's access the relationship and then go ahead and save. Now, just before we look at this and before we look at this working, sometimes you'll see places, things like this. So you'll see someone do this and go ahead and say imageable type. And they'll go ahead and say something like user, which is our fully qualified class name to our user, which we're going to change later.
01:37
Like I said, an imageable ID. And then they would grab the user's ID, assuming that the user is at the top here. And that's within scope. Now, there are some circumstances where you have to do this. Not very often, but I've come across instances where you do. But try to get away from doing that. It's not recommended. What we want to do is we want to eloquently save these relationships, set them together.
02:05
So what we're going to do is we're going to say user, we're going to say image and we're going to say save and we're going to save that image. So what this is going to do is it's going to obviously save this new image that we've created out here in the database. By the way, you can also say image make if you prefer to do that. Doesn't really matter. And then we're going to save it. And what that's going to do behind the scenes is it's going to go ahead and fill in the imageable type
02:30
and the imageable ID for us based on what we are saving this on. So it will infer that the user is a user model, obviously, and it will save them for us. So let's go over to the browser, give that a refresh. And OK, yes. So we've just got a mass assignment error. That's fine. We can go ahead and fix that up relatively simply by either going ahead and saying this is fillable or we can go ahead.
02:54
And set guarded here to false, which is typically what I do. OK, let's try that again. Give that a refresh. And there we go. So now you can see that is saved out that type in here. And it's saved the imageable ID. So in whatever circumstance in this scenario, you are uploading an image. You would go ahead and maybe read that, save it to disk, grab the model type you're using that might even come from your roots.
03:21
And it's going to work in exactly the same way. So if we say post and we switch this over to post, find one and we say post image, which again has the same relationship name, this is going to work in exactly the same way. Give that a refresh. There we go. It's landed up in the database with the ID, but obviously the different model namespace. And to be honest, for very simple one to one relationships, that is pretty much all you need to know in terms of saving them in the database.

Episode summary

In this episode, we're using a bit of imagination to picture a UI where users and posts can both have images uploaded and attached to them. The key focus here is on flexibility—whether we're dealing with a user or a post, we should be able to handle images in a generic way.

We walk through the process of associating an uploaded image with a user. Instead of manually setting things like imageable_type and imageable_id on the image (which you might sometimes see), we show how to use Eloquent's relationship methods to handle this for us. This makes the code cleaner and less error-prone.

You'll see in action how we can create an image, save it via the user's relationship, and Laravel will fill in all the necessary details behind the scenes. After sorting out a quick mass assignment error (with a discussion of fillable vs guarded on models), we repeat the same operation with a post just to prove it works no matter which model we're dealing with.

By the end, you'll have a solid grasp on saving one-to-one polymorphic relationships in Laravel, keeping your code neat and making use of Laravel's features rather than coding things manually.

Episode discussion

No comments, yet. Be the first!