Laravel 5.8 Tutorial From Scratch - e14 - Eloquent BelongsTo & HasMany Relationships

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back in this lesson we're going to be looking to associate our customers with a company and the first part of this is gonna be a bit of a review we're gonna create a model and a controller and then we're gonna get to the actual meat of this lesson which is eloquent relationships so stick around and you'll see how easy it is to have relationships with laravel and eloquent the first thing we're going to do is we're going to go to our console and we need to generate a model so we'll say PHP artisan make model and what's the model gonna be called remember our models are a singular version of what we are going to do so we're gonna have companies but our model is gonna be company so company and if we need the migration to go with it we're gonna add the - M flag so there we go so we've created a model and we've created a migration so back in phpstorm let's start with our migration so it's gonna be called creates companies table and so let's keep it simple I'm gonna add a string for name and that would be company name and maybe let's just add one more for phone just to keep it very simple we're gonna do just a name and if phone let's go ahead and hit save so let's take a look at our company model and it's going to be called company and that's inside our app directory just so we keep track of this inside our app directory here this company as I've mentioned before I almost exclusively always have my mass assignment protection turned off so I'm gonna do that now protect it guarded equals empty array and if you're unsure about mass assignment check out the previous lesson where we go into in-depth between setting fillable fields or setting the guarded property to an empty array so now that we have guarded off it is basically turned off completely up next I want to create a company so obviously right now I don't have a view for that yet so the easiest way is to boot up PHP artisan tinker and let's whip up a new company so C equals company and of course we can use this notation because we added our garden equal to empty array otherwise remember this will give you a mass assignment error so now we can say name just ABC company and we'll have a phone all right those are the two fields that we had added so phone one two three one two three one two three four all right so now we have a company in our database so so far this is all review right we've done all of this before so hopefully it's starting to feel a little bit better in terms of flow now here's the new part of this so this company is gonna have some customers right it's what we call customers so a customer to us has an Associated company so the way to think of that is we can say a company has many customers and a customer belongs to a company and that's exactly how we're gonna write it so let's head back to phpstorm now this is my company model and let me pop open my customer model I'll pop them open side-by-side so let's add a new relationship so this is how you do it in my company model right remember a company has many customers so we'll add a new public function called customers and inside this method return this has many customers class this public function is called customers and customers again is the plural of customer if you keep to this naming convention of singular to plural everything just works you can always override this if you don't want to follow the convention but for now just to keep it simple just follow the convention so public function customers a company has many customers and then let's write the inverse in our customer model public function company singular and we'll say return this customer belongs to a company and that's it just like that our customer is associated to a company and our company is associated to a customer we have one last little thing that we need to take care of and that is the foreign key in our database in order for us to associate a customer to a company that customer has to store a company ID so let's take a couple of step backs and take a look at our create customers table so far in this table we have the name the email and this active column that we worked on a couple of episodes ago so we need to add a new entry here unsigned integer and it's gonna be company underscore ID so that's the convention we have a company model and then on our customer we have to add a company underscore ID so this hit safe I know that I had already created a company and tinker we're gonna have to create that again so let's do PHP artisan migrate and I'm just gonna call the fresh and fresh will give us a blank database all together but we do see here we have our customers table and we have our new company's table so let's create a company now I'm gonna use PHP artisan tinker again PHP artisan tinker company creates and remember I can use the create method because I already set my guarded equal to empty array so we'll quickly say name ABC company and the other field we had was just a phone and we'll say one two three one two three one two three four alright so create that company for me perfect in our form in our customer form we need to add a drop-down so we can actually select the appropriate company for the customer that we are creating eventually of course we'll have to add a form to be able to add companies but for now we'll keep it simple and do this this is not for active this is gonna be for company ID and we'll say company and so for our select here of course we're gonna have to pass this data to our view so let's go to the custom our controller and let's see what we need to do here so our list is currently getting a list of active customers and a list of inactive customers so we're gonna need a list of companies and we'll do that simply by calling the all method on it we can always scope this down to active companies and stuff like that again we're keeping everything as simple as possible let's go ahead and pass that into our compact so let's use the blade syntax for each for each companies as company and then end for each and what we're going to have here of course is an option for each where the value is gonna be company ID and the name is gonna be company name alright let's check it out in the browser refresh and there we are so obviously ABC company is the only company we have in our database right now so let's add a customer we'll say John Doe John at example.com status active ABC company add customer and we get an integrity constraint violation the reason why this happened is because remember we are validating specific fields we did not update that in our controller so let's take a look at that now so right here we have name email active let's add a new one for a company ID and we do know that that is required all right let's hit refresh and there we are so we added this customer so check this out I'm gonna boot up PHP artisan tinker one more time because I do want to show you this relationship so I'm gonna fetch my company and we're gonna fetch that by saying company give me your first one so now we have company saved to this company alias so if I say company give me your customers there we are so we've associated John Doe to this company right here and we can do the inverse of that so we can say customer let's fetch the first customer and let's say customer give me your company so we are able to associate a company to a customer so I want to use that relationship right now to reflect that in our customer view so we were using the email but what I actually want to do is instead of the email I'm gonna fetch the company relationship so we'll say active customers company and give me their name so again active customer is fetching the company relationship which we established in our customer model let me show you that now right here but notice that we are calling it without using parentheses right we are not calling company parentheses it's company as a property and we'll take a look at the difference between calling it as a property or calling it as a method but for now just remember no parentheses so active customers company and give me the company's name is it safe and let's check out what we get so we are able to get that ABC company right into here that's pretty cool so in my view I actually want to add one more little section and it's just to prove that we can fetch the relationship in the other direction so let me add a row and for now we'll just keep it as a column of 12 inside and here let's just do for each company's last company and for each let's output the company's name in the h3 tag so company name and then I want to fetch all of the company's users we'll keep it simple and keep going with this there's a better way of doing this but for now we'll just say for each company customers and that's gonna give us a collection as customer we'll wrap this in an unordered list really quick here but list item customer and give me their name and then and for each let's wrap this whole thing in an unordered list there we go all right let's check out what we have now so right now ABC company has one customer John Doe and the way we fetched that relationship was by calling company and then fetching all of their customers and then we're iterating through each of those customers as customer and outputting that to an unordered list and then we have at the top we are using it to fetch the company's name so let's check that out so right up here we have our active customer we're fetching their company and fetching the name of the company again the relationship for that came from our model in this case this is the customer model we have this company relationship here established and in our company model we have a customer relationship here established in our migration for our customer we did add a company ID which is obviously a foreign ID for the customers table and this is what represents our company and then from there now everything just works I'm gonna add one more company and add a new customer to it so you can see it all working in the browser so let's add the first company first I don't have a view for that so I'm gonna have to revert back to adding it through here let me go up a couple of clicks here so we'll have a new company and this company is gonna be called the EF company now let's go back to the browser I'm gonna hit refresh and so now we have the ABC company and we have the d EF company let's add Jane Doe to the list and Jane is Jane and at another test com we'll make her inactive it won't make a big difference we'll hit add customer and there we are so we have Jane Doe associated with d EF company John Doe is associated with ABC company and we are able to fetch all of the customers for each of these companies to prove that I'm gonna add a third person here and we'll call them John Smith John an example is fine add customer so that we are John Smith and John Doe both belong to ABC company as we see it here and Jane Doe belongs to D EF company as you see it here so the has many and belongs to relationships are probably one of the two most used of the eloquent relationships but we're going to continue to work on relationships throughout this series and we're going to dive deeper through all of them just like we did with these two so keep playing around with these relationships and adding more tables and trying to associate them and when you're ready we'll move on to the next lesson
Info
Channel: Coder's Tape
Views: 78,607
Rating: undefined out of 5
Keywords: laravel 5.8, laravel tutorial, laravel new features, laravel best packages, laravel 5.8 tutorial, laravel 5.8 what's new, laravel 5.8 install, dotenv, php framework 2019, php what's new 2019, laravel, laravel 5, laravel from scratch, laravel for beginners, laravel mass assignment, laravel eloquent hasmany, laravel eloquent belongsto, laravel eloquent, polymorphic relationships, eloquent orm, laravel relationships, mysql relationship, belongsto, hasmany
Id: 3Oxfi3DLdkI
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Mon Feb 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.