Eloquent One 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 daria and i hope that you have a wonderful day the next few videos will be dedicated to relationships and eloquent since in my opinion it's incredible to use once you understand it in a relational database model which eloquent is it's expected that you will have tables that are related to each other so let's say that we have a table cars with our id name founded and the description now every car obviously has its own car model or multiple car models which you adjust your current card table by adding a new column model or would you create a new table well if you would add a new column it would look like this right we have our id name founded description and our model like i said the issue is that one car has many models and not only one so we would have multiple ids with the same name same founded same description but different models and that's not beneficial in a relational database model is expected that you will have tables that are related to each other and eloquent provides simple and powerful tools to make the process of relating your database tables easier than ever now in eloquent we call this an one-to-many relationship we can have one car and that one specific car has many models so our database would look like this we would keep our cars model just as it is so the id name found it in description but we create a new table called cars underscore module which has its own unique id but the car id which refers to the id in the table cars it has a model name and it has the timestamps there are two ways how we could handle our migration we could create it right inside of our current migration of cars so right below of it since it's related or we could create a complete new migration and for my demonstration today i will add it right below the current migration of cars that we have so it makes it a little bit easier but i recommend you to use a new migration instead of adding it right here let's create the schema so let's say schema create now inside the create method we need to define the table name so let's say car underscore models then we want to pass in the function just like what we're doing up here let's pass in blueprint variable table right outside of our parentheses let's add curly braces our car model has its own unique id so let's create a table increments and let's pass in the id then we have an integer so let's say table an unsigned integer with a name car underscore id which will refer to the id that we have right there we'll do that in a second we have another table which is a string of model underscore name let's add a table for timestamps the next one that we have is new so let's say that we have our table which has a foreign key of car id so let's say foreign and inside our method let's pass in car id so we're basically saying that the unsigned integer that we have is our foreign key we need to link it somehow with our cars table to the id so let's do that what we could do is to say access operator references to the column that we have inside our cars called id on so on the table cars so let's do that let's say cars and this can be it so we're basically saying our foreign key is car id which references to the id on cars what i want to do is to add one more method so let's say on delete and what we want to do on the lead is cascade and let me align it actually so it looks a little bit better all right why do we add the on delete that we have right here well if you delete the car you don't want your car models to be in your database since it's not being used because the id does not exist anymore and that's what the on delete does if you delete a specific car it will automatically delete the car models related we can add one more value inside the parentheses inside of cascade but for example right now let's just keep it as it is but just in case you don't want to delete it you could also set it equal to set no so whenever you delete a specific car it will set the car model's car id to no we will keep it as cascade what we need to do right now is to migrate it so let's hop to item let's say php artisan migrate all right it's saying that nothing is there to migrate so what we need to do is to say php artisan migrate rollback which will roll back our migration and then we can migrate it once again so it migrates to create cars table so let's hop to my sql let's say show tables and you can see that we have car models and our cars table when i created a video about introduction to eloquent we didn't do anything with our show methods right here the reason why is because i want to use it right now and before we continue on guys i want to start using my instagram more so if you haven't followed me on it and you are interested in more personal stuff about me i have added the link in the description so go check it out and give me a follow i want to show you how we could set our models manually first so let's hop to the command line interface and right here let's say desk car underscore models to see what we have as you can see our id is pre which stands for primary but our car id is mo mo basically tells your database that there can be multiple ids in this column that have the same value and that's correct right because a car can have multiple models there's also another one which you don't see right here and that's uni so let me write it down uni which stands for unique we need to do something inside our model that will do something with the data that we have since we already have our car model we need to create a new model so let's say php artisan make me a model called car model let's hit enter and our model has been created successfully if that went a little bit too fast for you let me rewind how this works is that the car model belongs to a specific car and the car has many models and we need to fix this right inside of the models that we have so let's go back to visual studio code and let's start off with our car.php so our car model what we need to do right here is to create methods that will return the right data based on our tables let's create a method let's say public function and the name is pretty tricky because it needs to be related to the associate table that we want to use this could be underscores camel cases or whatever but in my opinion try to keep it the same as the model name for the one-to-many relationship we have the opportunity to get multiple models so let's say that we want to call it car models instead of a car model so what do we need to do right here we need to return something which is this a method called has many parentheses semicolon inside the parentheses we need to pass in the model class of the associated table so in our case we want to associate car model called a common class with the car model that we have and we don't need to add the full pad right here because requiring eloquence complete model what we have done right here was defining the relationship method for the car model we need to create another method for the car model that's related to the specific car so let's open our carmodel.php but we could actually copy our let's say protected table and primary key let's go back paste it right here table is car underscore models and the primary key is id as well so what's next we need to create a new method so let's say public function and once again the name needs to be related to the model that we want to associate it with so car so let me add it as a comment a car model belongs to a car so what we need to do inside our method is to return this and i'm basically repeating myself again one model or multiple belongs to and inside the parentheses we need to pass in the model that we want to associate it with let's say car column column class save it and let me actually save my model as well all right we need to add data inside our database and there are multiple ways how we could do this we could do it directly inside of our controller but what i just want to do is to go to i term and add it manually so let's say that we want to insert into cars because we need a specific car the title found founded description comma values audi comma 1908 and this is my audi as the description oh it's not title excuse me it's name my bad all right let's select all from cars this point well let's desk car underscore models all right so we need to insert into car underscore models just the values car id and model name so let's say car underscore id model underscore name values and as you could see our id of the rd is one so one comma inside single quotes a1 which is the model of the audio if you're not familiar with cars hit enter one row has been affected let's hit the arrow up and let's change the second one to a3 and a5 three is enough so let's select all from car underscore models and this is fine you can see that our car id has multiple values which are the same and right now we're ready to output the data on our page so let's go back to visual studio code let's go to our cars controller and the show method since we've used the resource flag you can see that the id has been passed in automatically well actually let's go to our ui and let's change that first so inside our index let's go to the car name let's copy it let's create an answer and let's place it right inside of the android let's give it the href of forward slash cars forward slash that specific car with the id save it let's go to chrome refresh it and you can see that our audience is clickable but let's add a cover as well so in our h2 let's say hover text that's great that's 500 save it let's go to visual studio code and since we've used a resource flag inside our cars controller our show method has the id passed in so what we could do is dd id just to double check that everything works fine refresh the page click on audi and in the uri you can see that the endpoint is one and one has been printed out so this works fine this is always a double check that i do when i work with laravel let's get rid of our dd because we could create a new variable car since we're getting one single car back set it equal to the car and find everything of that specific id now let's return a view that we don't have yet to cars.show and we want to pass in an object and not an array so with car as that specific car before we continue on let's do the car again to see if everything works fine save it refresh the browser and i'm not opening my attributes but you can see that one car has been returned now we're ready to create our view so let's go back to our cars create a new file show.blade.php let's say extends layouts.app let's create a new section called content and section let me close off my migration and my controller because we could copy most of the stuff in our index so let's grow up we could use our header but instead of saying cars we could print out the car name variable car name let's go to chrome well let's remove our dd first in our controller excuse me save it chrome refresh it audi has been printed out let's go to our index and we don't need to add a new car button but let's copy this entire class all right let's paste it right below our div let's scroll up we don't need the loop we don't need the edit button and the form and the closing div of course we do need to found it we don't need a name since the name is printed out at the top and we do need the description save it and let's go to chrome we need to remove our end for each refresh it this is alright let's style it a little bit remove the width because we don't need it let's say that we want to text center everything and this looks pretty good right now but what we could do is to add some lorem ipsum excuse me looks a little bit better than one piece of text all right let's scroll down let's copy you don't need to do this it's just for myself let's edit it let's change the text all right let's click on the audio again all right and right now we're ready to print out our models what i want to do is to go right above our hr let's create an unordered list with a paragraph of models let's set the class equal to text large text gray 700 and padding y-axis is 3. right below our paragraph we want list items but before we do that i want to loop through my models first and i don't want to create a for each but i want to add a for else what that went wrong and the reason why i want to do that is because there might be a chance that a car does not have models so we want to print out something if there are no models right so first if there are models create a list item with a class of inline italic text that's great that's 600 padding x is 1 and the padding y is 6. so right here we want to print out our model name but before we do that let's fix our loop we want to loop through our car but we want to access another property called car models so we're accessing the method that we have in our car model as one single model in our list item we could basically say model gave me the model underscore name if it's empty print out a paragraph of no models found save it go back to chrome and you can see that the models a1 a3 and a5 have all been printed out you might wonder why we didn't use the access operator to access our model name and that's because we're returning an object so we can't use the access operator and this was it thanks for watching this video if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel click on the subscribe button
Info
Channel: Code With Dary
Views: 12,484
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, eloquent in laravel, many to many laravel
Id: l8m0rYR7W8w
Channel Id: undefined
Length: 16min 31sec (991 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.