Laravel Pivot Tables: Simple to Advanced Many-to-Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we're gonna have a deep talk about many-to-many relationships and pivot tables in eloquent and larvae and I will show you simple example then we get more complex and more complex to customizing pivot tables adding timestamps adding another model and stuff like that so for those of you who are not that familiar you will learn about pivot tables and for those of you who use that maybe you will learn a thing into which you didn't note before and this is an example of a pivot table of a many to many relationship between projects and users so every project may have many users and at the same time every user may have many projects that's why it's many too many and let's go to the code how did I create the migrations first migrations users table comes from default laravel I didn't change anything here then projects table is a new table with our case it's only one called name nothing really fancy here but this is the pivot table the intermediate table between those two and there are a few things that you need to know here first the name of the table project user to be able to use full are eloquent magic as I call that there are specific rules how to call that table it should be alphabetically so it cannot be user project also it has to be singular so it cannot be projects users so basically you take two tables order them alphabetically and singular and that should be the table name of pivot table and then columns also have to have name of single of that table underscore ID and by the way this syntax for an ID came with laravel 7 so before we had to write two lines of code for every column so column in a foreign key and now it became for an ID with constraint now how does that look in the model we have project model and this is how we describe belongs to many relationship if we describe everything correctly as I mentioned here with names then we don't need to add any parameters to here it's a short one liner and then if you want to add a user to a project I will show that in a seeder so I've seeded some data I've seeded 10 projects and for every project I've seeded three-run double users but this is the line where you actually attach the user to the project and this is the main benefit of that a relationship configuration that then you can use eloquent and laravel functions and write small amount of code and under the hood it will take care of all of the relationship and all of that and then I've created a page home blade which just bless the project with their users it's just why PHP file - home controller and unless open home controller it's just project with users get and in the browser it looks like this so list of the projects and list of the users for every project so this is more or less the basics of many-to-many and pivot tables and now let's go deeper first customizing this thing what if you do want to name the table in a different way then you need to configure that so for example let's create at projects users and then the field would be projects ID users ID I don't advise that but maybe you do have that table already from somewhere so let's say you have these names and then in the management relationship here you need to specify the table name projects users then field of projects ID and the field of users ID my font is probably too big in phpstorm here now so this is how you define that and let's ring migrate and recede all the data and see if it doesn't break so we have PHP our doesn't migrate fresh seed all good here in the database let's refresh the tables we have projects users and in home no error new data so if you specify everything correctly in the belongs to many relationship then you can name your table or columns however you want and now let's add more things to our pivot table one by one and we will go deeper and deeper first time stamps as you can see here in the database table there's no timestamp but maybe you do want to know when that project was assigned to some user to do that you need to do two things migration you need to add the time stamps calm columns actually time stamps and then in the relation you define that with timestamps and then if we again migrate fresh seed it should see the data refresh with timestamps by default and now what if we want to show that timestamps here so when that user was created or attached to the project in fact to do that in the blade file we will add this so user pivot created add if you want to work with any additional column in your pivot table this is the keyword so you have project users and then from user you call the pivot table the intermediate one and let's refresh this is the result but what if we want to add more fields not only timestamp some custom field for example if that project is the main for that user so is the user the leader of that project manager or something so we add a field again probably the same thing so with x x we add table Valene for example is manager and default false so that's migration then in the relationship we need to specify with pivot and then we add array of fields which would be additional in that pivot table so in our case it's one thing it's manager and then when attaching the user to the project so in here in the seeder we may specify additional fields and for that we will attach the user one by one so for each users as user we will go attach one user with parameter is manager equals let's put random 0 1 and let's we see that success and let's see what we have in our pivot table now we have a new field is manager which is randomly 0 or 1 and same thing if you want to show that somewhere is manager and let's refresh the page zero or one here so this is how you add any custom fuel to that pivot table next thing is maybe for some reason you don't like that name pivot maybe it's not that readable for people who don't know lerigot so you can rename that while specifying your relationship here you can specify as and you can rename that to whatever you want so for example project user and then you do project user here so whenever you need that pivot table columns you just specify project user because that's how you will name that and if we refresh nothing changes it's all working next thing what if we want to specify a specific relationship to managers only so we need to get only managers so we can add a condition to our relationship or in fact we'll create a second relationship not users but managers so managers still belongs to many we will remove that name and add where pivot and this is the same as adding where conditions so is manager one and now if we query the managers with project not users but managers we have another relationship right then we can do for each project managers as user and we don't need to specify that and let's see what we have as you can see this project doesn't have a manager and this has two managers but you get the point you see only managers here so you can specify two relationships separately like this one and also there are two methods which I won't show here but there's work pivot in and work P but not in so for example if you have not a boolean but any other field like integer you would be able to have something like that so call them for example priority of project is one or two and final thing you've probably noticed that during this video we didn't create special model for the pivot table so there is a model project model user and they somehow tied together but if you want to perform some operations on the pivot table specifically sometimes it does make sense to create a model and there is a specific type of model called pivot so we can do something like artisan make model project user and if we open project user as a model we need to rename that to pivot pay what and that's it why would you do that what would be the use case let's change our manager field to the relationship to actual managers so let's assume that every project and every user has their own manager or supervisor within that project and that would be relationship to user table and in our blade we would like to show every project every user and their own manager there so let's do exactly that in migrations we change bullying is manager to for an ID manager ID and instead of constraint now we reference reference ID on users so have that manager ID then in the relationship we don't have that is manager and let's remove that one as well and we will add using using and need to specify the class which is project user class and then we can add something like L upon stuff to the project user including public function manager which will be relationship returns this belongs to user class with foreign key manager I team now in the seeder let's change manager ID to user in random order first an ID let's ricci that seems yes full let's check the database now and we have our manager ID and now let's show that manager to the list we add with pivot so when you specify that we have that field manager ID and then in the blade instead of that manager we will add a supervisor so change that to users and then we can add user pivot manager name so see what is happening here project has users users calling the pivot pivot is calling the manager relationship and then the name and this is the result so that's how you can use a separate model for the pivot table and then inside of that model you can have relationships so that was an overview of pivot table structure various customizations and additional less-known features if you are hungry for more on the same topic of eloquent have a special course called eloquent expert level it's available on teachable and the link is here so in that course I go through a lot of customizations of eloquent in general not only pivot table so you can check that out also subscribe to the channel hit the bell button to be notified of new videos by email and see you guys in other videos
Info
Channel: Laravel Daily
Views: 63,833
Rating: undefined out of 5
Keywords:
Id: V5xINbA-z9o
Channel Id: undefined
Length: 12min 24sec (744 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.