Eloquent Many To Many Relationship | Laravel 8 For Beginners | Learn Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you have a wonderful day in this video i want to focus on the many-to-many relationship now compared to the other relationships i'd say that a many-to-many relationship is probably a bit more complex so let me show you a little example right there we offer table cars and a new table called products a car brand can have multiple products think about auto mobiles electric vehicles automotive parts looks to review close pickup trucks each product type can be related to many cars audi and mercedes can both be your auto mobile or an electric car in fact they are both in auto mobile so what would you do would you keep our current table cars and add a product id and for a products table at a car id since a single product can't have a column cars id and a single car can't have a product type id because remember we need to create a many to menu relationship what we have on the screen does not work so let me show you a better solution what we need to create is something which is called a pivot table and right now you might be wondering where the pivot table is it's a table that connects the two tables so we still keep our table cars and we also add our table products and we don't link them but in between we place a pivot table now for the naming of our pivot table i prefer to place the two singular table names together ordered alphabetically and separated them by an underscore so since we're linking our cards and products together our pivot table should be car underscore product you don't need to pass in any strings as the data type you only want to pass in the id of the car and the id of the product now that i've showed you a demo let's hop to the cli and let's create a new migration and model first we need to create one for our product so let's say php artisan make me a model called product dash m let's go to our migration let's actually open it up all right we want to keep our table name products we don't want to change our table name products is fine we want to keep our id well let's actually change it to increments let's give it a name of id and in here i want another column which is a string called name and i also want to keep my timestamps and that's it we don't need more so let's save it let's go to item and that's right now php artisan migrate our migration has been created now for the pivot table we do need a model and migration as well the model is there to interact and the migration is there to create a table so right here we could say php artisan make me a model called car product dash m all right let's open the migration as well let me scroll down and in here we need to create two columns for the ids that we need so let's get rid of the first the first column is an integer called car underscore id and it's unsigned let's actually duplicate it because the second one is our product underscore id which is unsigned as well now here comes a pretty weird part both our car id and project id are foreign keys so what we could do is to say table foreign let's say car underscore id references to id on the table cars and on delete cascade it let's end it with a semicolon let's copy it and let's paste it right here let's change the second one to product underscore id references id on products and on delete cascade everything now before we migrate it laravel will automatically define your table name sometimes you just want to define your own table name so let's change our table name to car underscore product save it let's run php artisan migrate our table has been migrated now let's go to mysql let's say show tables we have our car underscore product and our products let's desk products to add it so what we want to do is to insert into only the column name the values let's say electric vehicles insert into my bed products name and the values is electric vehicle all right let's hit the arrow up let's add another row called auto mobiles and let's add the arrow up one more time to add luxury cars now if we select everything from our table products we have three new values in here which is all right and let's also select all from cars so our car has an id of one and our products has three new ids so what we could do is to insert into car underscore product let's say car underscore id product underscore id the values are one comma two so audi is an auto mobile let's hit enter let's say that audi is also a luxury car all right let's also add a new car in our database so insert into cars let's say the name founded description values are mercedes 1926 and let's say this is my mercedes for my description all right select all from cars again what we could do is to basically hit up again because we want to insert into car product the car id is apparently 7 as you can see let's say that mercedes is a automobile and a luxury car so let's say 2 arrow up and three alright we're ready to define our relationships right now so let's go to visual studio code let's go to our car.php let's create a new function public function products and here we want to return variable this all the products that belong to a specific car so belongs to many which takes one param and that's the product model so let's say product common colon class let's save it let's close all of our migrations because we don't need it let's go to our cars controller and let me show you how we could print stuff out now right below our variable car let's say fire underscore dump let's say variable car products save it let's go to google chrome refresh the page and you can see an entire list of data inside our collection what we could do is to say command f or control f and let's write down pivot you can see that we indeed return data from a pivot table and like i said just like the has menu relationship we get access to a collection right here of the related items but this time it's from both sides so from the cars and from the products so what i want to do right now is to print it right in the ui nothing fancy but just to show you how we could print it out so let's go to our show.blade.php again now let's go right at the bottom above our hr let's say paragraph let's give it a class of text left let's say product types let's create a for else loop we want to loop over car with all products as one single product if there is a product let's print it out so product name otherwise create a paragraph and let's say no car product description or something save it let's go to our cars controller let's get rid of our fire dump save it google chrome refresh the page and the products types are auto mobiles and luxury cars next to the many-to-many relationship there's also an inverse many-to-many relationship we have only printed data from one site so from the cars since i haven't got a ui where to print out the product types i want to show it to you right in the controller so before we do that we need to create our relationship inside our product.php so right here let's say public function cars we want to relate our cars to the product so we want to return this belongs to many car it takes one param which is the car calling collin class save it let's hop to our cars controller right inside of the show method we could create a new instant called products and let's set it equal to new product and what i want to do is to find data from this specific car so we want to pass in the id oh excuse me we don't need new my bad that's print underscore products save it go to chrome refresh the page and we have a object of product with every piece of data that we need that is related from our product to our cars like i said before the many-to-many relationship is unique because it's the first relationship that has a pivot table now this was it for this video if you do enjoy my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button
Info
Channel: Code With Dary
Views: 7,447
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, laravel tutorials from scratch to advanced, laravel many to many, many to many relationship
Id: 2oFNu_RhTt4
Channel Id: undefined
Length: 10min 20sec (620 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.