Laravel Many-to-Many: Extra Fields in Pivot Table

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to show you how to solve one problem in many too many or belongs to many relationships that is if you want to add extra field to the pivot table i will show you how to do that on a database level in eloquent model and also in the form this is most important probably so this is our starting position this is generated by our quick admin panel so we have recipes and we have ingredients recipe belongs to many ingredients and when you create a recipe you may choose many ingredients and then if you save what happens in the database if we refresh there's a recipe soup and then there's a pivot table ingredient recipe so we save three ingredients into recipe id one and in the code this is what again our quick admin pattern generates by default we have a select with ingredients as array we also use select two library.js library to make it more pretty but from html point of view it's an array and then in the controller when storing the data we store the recipe and then we store ingredients with sync eloquent function used for belongs to many so if we go to recipe we see ingredients as belongs to men but in reality when creating recipes here in the form you probably want to specify how much of each ingredient is involved in the recipe so how many grams or kilograms or items or quantity so let's add that quantity field to the database to the eloquent model and then to the form so first we need that field in the database and we do php artisan make migration add amount to ingredient recipe table and then we should have the migration generated database migrations here's our new migration and we add one field let it be string because amount could be in something like grams or kilograms or text so amount then we need to add that in the eloquent model so for recipe belongs to many we need to specify with pivot and then what fields are inside of that pivot table so we have only one amount and now we're ready on the database level now the form create blade will change from just select to checkbox for each ingredient and then input field for each ingredient so i've prepared that code in the background to save you time we have a table for each of ingredients we have a table cell with checkbox with data id which we will use in a javascript to check the event of checking and unchecking and then we have a text box input with the same data id for the amount and by default it is disabled visually it looks like this so again we add a recipe soup for example and then we can add meat potato or whatever and ingredients in the controller come and create ingredient all next step is to make them clickable so whenever you click the amount should be undisabled and in the create blade we will add a section at the bottom four scripts so section scripts it's a jquery code you can do it with whatever other js framework or without framework it's a pretty simple effect so whenever the ingredient enable class check box is clicked we get the id and we enable or disable the text box and set the value and that section script comes from the main blade file so that create blade extends layouts admin and if we go to that admin at the very bottom there's yield scripts for whatever custom javascript we want in our case for quick admin panel generated code it's jquery with data tables with quite a lot of javascript for that but we can add more javascript in whatever blade we want so if we refresh the page we enable and the amount is enabled if we uncheck the amount is disabled and not even that we can add the amount we uncheck and this should be empty and now finally how to save the data so from the create form we have ingredients as array but not only any array it has the element keys defined by ingredient id so it's not just zero one two and three it has exactly the ids that we need to sync so in the controller instead of just sync request input like this we need to form the ingredients and those ingredients will be you may perform a for each operation or some other structure but i will use eloquent collections so put the array into collection like this and then i will use a method from collection called map function ingredient return amount equals ingredient like this and then we sync ingredients in efficient laravel documentation it's this syntax so we can sync belongs to many with id and then array of additional fields that's what we defined in with pivot here so any field that is described here can be used like this here so to make sure i will do dd ingredients and let's see what we have our form let's refresh let's add a soup with meat and potatoes three potatoes and 200 grams of meat and the result is two items so id one with amount 200 grams and id2 with amount three and this is exactly what we need so let's delete that dd and let's refresh and we have an error of course we haven't run the migrations phpr doesn't migrate that's a typical error i make in a lot of my videos forgetting to run migrations okay so two recipes were saved without ingredients and here we have with ingredients and let's check the database if it was saved correctly we refresh and as you can see the amount is present so this is how you can transform the simple pivot table with multiple select to a form with checkboxes and syncing extra values i've described that in more details in a blog article for our quick admin panel also describing the edit and update some more refractoring needed so it's all detailed here with the code so you can copy from here i will link that article in the description of this video and generally i suggest you use our quick admin panel to generate code and by doing this you support this channel and then the more money quick admin panel makes the more time i personally have to shoot free videos like this one see you guys in other videos
Info
Channel: Laravel Daily
Views: 35,794
Rating: undefined out of 5
Keywords:
Id: v6kX9U5xxqQ
Channel Id: undefined
Length: 6min 49sec (409 seconds)
Published: Wed Nov 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.