Laravel 5.8 – From The Ground Up
Lesson 48 of 52 · Laravel 5.8 Tutorial From Scratch – e48 – Eloquent Relationships Many To Many Part 2 (BelongsToMany) · Education
Laravel 5.8 Tutorial From Scratch – e48 – Eloquent Relationships Many To Many Part 2 (BelongsToMany)
welcome back to part 2 of the many to many relationships in laravel if you missed the first part check out the first part before you move on to part 2
in this part we're gonna be talking about attaching data to the pivot table and this is useful because sometimes there is relational data that is only relevant if two things are in sync with
each other so the example we're gonna use is you have a user and then you have a role but I also want to know who gave that user the ability to perform that action meaning who was actually
responsible for the sinking of that so that's what we're gonna be working on we're gonna add another column to our role user table and then that's gonna store a name and the name is gonna be
the name of the person who performed the action to allow that user to perform that role it's a very common thing to do because a lot of times you'd want to be able to know who granted the permission
for someone to do something in your application so let's work on that now let's go to the migration for the create role user table and let's add a new column here and we'll say table and the
new column is going to be a string and the string is just gonna be named as in the person's name alright so now let's do a quick PHP artisan migrate fresh so we have a nice and fresh database and
there we are let's jump the table plus and of course everything here is empty except now we have this new name so how do we do that alright it's actually quite simple let's
continue with the example that we had going on before this one let's comment that out let's get rid of this there we go alright so we'll just go back to just
using sync so we have a user we're grabbing the first user from our database actually that reminds me we actually don't have a user anymore so we need to make one now let's jump to PHP
artisan tinker and just make one really quick factory app user class creates perfect so now we have a dummy user great alright so now that we have a user we're going to fetch that user and then
we're gonna attach this role with ID of three which actually reminds me we also don't have any roles anymore let's take care of that now very quickly rolls let's
just add one and we're gonna say remove users as that's going to be the role and of course that has an ID of one so let's go back now we can say the following instead of just passing in an ID let's
actually use the ID as the key and we're gonna say one and then any relevant pivot data is gonna go right here who performed this action I will just save Victor so Victor performed the action of
attaching user number one to the role with an ID of one great let me hit save let's go back to Chrome hit refresh and now let's check out table plus so if we go to our row user table we hit refresh
we now see though of course we attached role ID one with the user ID one but now we have this other data attached so Victor was the one that was responsible for attaching that together so that's
pretty cool now how do you retrieve this data let's go ahead and just comment this out for now so if we grab their user actually know what we already have it up here we'll grab our first user so
then we can simply dine dump user give me your rules and if we go to Chrome there we are so user roles of course we know that the one role that this user has is just the remove user's role but
where is Victor where is that column how do I grab that pivot okay so there are actually several ways to do it but I'll show you the easy way to do it so we have this R users roles let's grab the
first row that way we're only working with a single row let me show you that in Chrome there we are so you see we have a role instance in the role instance of course
like I said is the remove users so instead of having a collection now we just have a single role alright so now that we have a role let's just call pivot on that role and let's check out
what we get refresh and there we are so we have a role yeah that's pretty cool right so we have the user ID in the role ID however we're still not seeing that name column
how come we're not seeing that well that's because we haven't told laravel about this extra data all right so to do that let's go back to our model this user has many roles it belongs to many
roles and it does have with pivot it does have name all right and we're gonna do the exact same thing to our other role model all right paste so there we go now we've told
laravel hey there's an extra piece of data on that pivot table that I want you to grab with you so let's go back to Chrome hit refresh and now there we go so now laravel is aware of that name so
now we know that the person responsible for joining these two things together is Victor great in the same manner if we go back to here we can't just call at this point just named so pivot name it
refresh and there we go that's how you retrieve any additional data from your pivot table this is very useful and you can have as many columns as you want and again the pivot table is going to hold
any data that is only related when those two models have a relationship so any data that is only pertaining to that relationship is what would go inside those columns so this is a pretty cool
example and just shows just how powerful level and eloquent is when it comes to databases so with that we're going to conclude the explanation of the many-to-many relationships and laravel
go ahead and implement that in your application and get comfortable with it and when you're ready let's move on to the next episode
Eloquent offers a simple way to declare database relationships. In this episode, we are adding additional data to the pivot table and storing the name of the user that performed the sync of those two resources.For the best experience, follow along in our interactive school at https://www.coderstape.com
Code for this Lesson
https://github.com/coderstape/laravel-relationships/tree/many-to-many
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!