This episode is for members only

Sign up to access "Eloquent Relationships By Example" right now.

Get started
Already a member? Sign in to continue
Playing
26. Intermediate table columns

Episodes

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

Transcript

00:00
When you're attaching with a many-to-many relationship, you might also need to add additional information to each of the unique attachments that you have between two models.
00:11
So that sounds complex, but basically what we're going to do in this episode is allow a topic to be attached to each of these courses. But for each specific attachment, we're going to give the version of the topic that we want. For example, if we were working with Laravel 9, we would be able to store that this topic is Laravel, but it is also Laravel 9. We might have slightly older courses that use Laravel 5, in which case we could attach version 5 to them.
00:40
So let's look at how we do this first of all in code, and then we'll update our example where we have our attachment just here, so we can include this additional information and send it through as well. Okay, so let's go over to our database first of all, and let's have a look at what we've got. So we've got course topic. We already know that we've got these created and updated updates, which technically we don't need unless we really need to know that we want timestamps in these.
01:08
So we'll take a look at that first just to give us an idea. Now, to demonstrate this, if we go over to where we're attaching stuff, so let's duplicate this over and go over to here. Let's attach Laravel to here and give that a refresh. Now, in our database, the created and updated update have not been filled.
01:26
We did fill them manually earlier. But what happens if we want to include them? Technically, they are additional data on our pivot. So to do this, we're going to go over to our course model, and we're going to chain onto our belongs to relationship with timestamps like so.
01:42
That's going to solve the issue. So now when we go ahead and attach something else, so let's go back and attach another topic, you will see that works. But inside of here, the timestamps have been filled. So that is kind of what we're doing here.
01:57
We want to fill in this additional data when we attach something to a course, attach a topic to a course. So let's get rid of these two examples again, and let's look at adding in an additional column to these. So we're going to make out a migration here, and we're going to add a version column to, and let's just check the table name, course topic. So course topic table.
02:22
So add version to course topic table. Let's head over to that migration. So add version to course topic table, and everything's been filled in for us just because of the way that we named this. Now we can just attach that in there.
02:36
So let's leave this as a string just for now. So we'll say version. Really importantly, we might not want a version, so we're going to go ahead and set this as nullable. Otherwise, we would be forced to add this in at the database level every single time.
02:49
And then for our down migration, let's go ahead and drop this column in case we need to revert this back. Okay, so there we go. We have added a version column to our pivot table. Let's go ahead and run our migrations here, and let's take a look at see what we can do.
03:07
So nothing will have changed here at the moment. So, for example, if we head over here and we attach something, nothing is different at all. We've got the version in there, but of course it's nullable, so it is just set to null. But what we can now do is go ahead and specify this within code.
03:22
So if we head over to our web routes and go down to where we're storing this, so just inside of here, we can pass this as a second argument to this. So let's go ahead and add in the attributes that we want to store. You can see that the attributes are here in the method signature.
03:40
So we're going to go ahead and attach a version here, and let's just set this to nine just as an example. Of course, this will be set to all of them, but it will work. So let's head over here and attach Laravel, and let's go back over. Nothing has changed, but now in the database, we have nine inserted into here.
03:58
So that's really useful, and it's added some specific information that relates just to this attachment between course and topic. So the question is, how do we actually output this data? Well, we want to probably show this next to each of the topics under each of the courses. So let's head over to our index.blade.php file and see how we can access that information and add it next to here.
04:23
So a really useful thing to do here when you are just testing out is just to dump out the entire topic itself. So let's just dump the topic and see what information we get here. So you can see we've got an object here which has been converted to JSON, which we can kind of ignore. And you can see we've got this pivot attribute in here now with the course ID, the topic ID created out, and all of that other stuff.
04:47
But notice in here, we do not see that version. So how are we going to access this? Well, we're going to head over to our course model, and we're going to do exactly the same thing as we did with timestamps. But this time, we're going to say with pivot, and then we're going to give an array of columns in here.
05:04
It doesn't have to be an array, but I like to define it as an array. And we can just choose what pivot columns within that pivot table we want to be included in that object. So if we come over and give this a refresh now, sure enough, you can see that the version is now included in here. So with that information, what we can now do is we can access topic and then pivot and then version like so.
05:29
Let's go over, give this a refresh. And there we go. We have nine next to Laravel. So in reality, that would probably be next to here.
05:37
And we might want to do something like V and then the version just inside of there. So as easy as that, we add a column to our pivot table via a migration. We go ahead and make sure that this is included in here. And we insert that by doing this and passing it through as a second argument.
05:55
Now, let's say that the pivot data here is not included. So let's just manually for now attach another topic to this course, but not include a version. So we'll set that to the default, which is null. Let's come over and see what we get.
06:08
Well, we just end up with showing nothing. So we can add an if statement to wrap around that over in here to check that it does actually have this. So we can go ahead and add an if statement in here and just say if topic pivot version. And we can go ahead and end the if in line here, or we can do whatever.
06:27
And that's not going to show that wrapper if it doesn't exist. Okay, so now that we've done that, let's pull this together in a practical example where we actually pass this data down. Now, we're assuming that versions are freehand. You can just write whatever you want.
06:40
We're not going to add any kind of validation to these. Let's actually pull this together into that practical example. So I'm going to go ahead and get rid of the eloquent attachment here. And I'm going to add eloquent in to here via this form that we have here with another input.
06:55
So if we head over to the file that we have just here and we add in another input, let's just do this directly next to the select. Obviously, it doesn't look great. And we'll give this a name of version. And let's just take a look at that.
07:13
Okay, so that will do for now. So it should now be able to type into here and then access it here and then store it. So here we can go ahead and just access request and version. And now what that means is we can very easily type something in here and it gets attached.
07:29
So let's attach to this course eloquent. And, of course, the version doesn't really matter necessarily here. But let's click attach on 10. And there we go.
07:39
That has been attached with that additional pivot data. So obviously you can do anything with this additional pivot data. This is an example that specifically relates to courses. But once you get to the point where you feel like you need that additional pivot data, you now know how to add this in.
33 episodes4 hrs 18 mins

Overview

Eloquent is Laravel's ORM (Object Relational Mapper). In simple terms, it's how your models work with the database.

The good news? There's a bunch of powerful relationship types available. Our task is to learn when and where to use each one.

In this course, we'll cover each basic relationship type, how to access related models, and then insert, sync, update and delete related data. Oh, and we'll build a practical example for each relationship type, to really make it stick.

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

Comments

No comments, yet. Be the first to leave a comment.