10 - Laravel Basics - Laravel Relationships

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
next up we're going to cover relationships and relationships allow us to create a relationship between two tables so say that we have a post table and then we have a comments table we know that a post has many comments and we know that a comment belongs to a post so in our example we're going to say that a customer has many orders and a order belongs to a customer so since I know that I want to create a relationship between two tables right now we only have our customers table I need to create a new table called orders so I'm going to create a new migration for our orders table so I'm going to run the PHP artisan make migration create orders table and that would help if I spelled artists incorrectly so let's go artisan and there we go we just created our new migration so now I have my new migration right here and we have schema create orders we have an ID and the timestamp so I know for our orders table we need specifically two columns and one is going to be the name of the order and the other one is going to be a reference to be specific customer so we probably would want to have a lot more information inside of this table but for simplicity sake we're only going to include those two columns so I'll start off by creating a string and I am going to just call this name so this is the name of the order and then I'm also going to create another table with an unsigned integer and I'm going to call this customer underscore ID so this is going to refer to an ID of a customer and that's how we're going to make the relationship and if you're not familiar with all these different types like string and unsigned integer you can go over to the laravel documentation and look at migrations and let me see if I can quickly find this yes you'll see all these different types right here that you can use ID for an ID bigger increments big integer and you can just go through all these and then read the description over here and it will tell you you know how you can use it and when you may want to use it so now that we have our migration ready we can go ahead and run PHP artisan migrate and you'll see that we get the message that we migrated the orders table so let's jump back over to the database and take a look so we have our orders here which has ID name customer ID and of course we have our created at and updated at fields and these timestamp fields are just default with laravel and a lot of other frameworks out there do this it's just a good way to be able to track once that Row was created and once it was updated okay so we have our customers let's go ahead and just manually add some orders in here so let's say that we have a new order called we'll say a new MacBook and the customer ID I know that Tony has an ID of one so I'll go ahead and add that and now I can say that maybe Tony has another order and I will just say new bike an ID of 1 and let's give in order to let's say John so John is going to get a new TV and the customer ID is going to be too okay so I can save that and if we need to refer to this or burstable remember we need to create a model so I'm going to go ahead and create an orders model so I can run PHP artisan make model order and now jump me back over to the code I can look inside of my app folder and I have a new order model right here so what I could do to just test this out is I could create another closure and maybe I just want to get all the orders so I can say get orders and I'm just going to create a quick closure just so we can test this out so I'll say orders equals and remember this is in namespace app / order and we want to get all those orders and I'm just going to return a response in JSON format so I'm going to save that and I will go to application dot test slash orders and you can see in JSON format we have all the orders that are inside of our table okay so now what we want to do is we actually want to list out the customer and then we want to create the relationship so that we say this customer has multiple orders and we want to display them out so let's go ahead and create that relationship and again if you need to learn more about relationships and anything in general you'll want to check out the documentation so we can check out relationships right here and I know that the relationship between my customer and orders that a customer has a multiple order so has many so I can go here and say one too many I can see that I can use this return has many so if we had a post table we would say that the post has multiple comments but in our case where we have a customer table and that customer has orders so I'm going to create a function called orders that returns many orders so I will go back to my customer model and create a new function called orders and elver returned this let's see that says has many and we have mini app slash order okay so let's jump back over to our home view and you can see here that we're listing out all of our customers but maybe we also want to list out their orders so what we can do is actually we can run another for each and check this out I could say customers or customer orders as order and maybe in here I just want to create a paragraph tag and list out the order name and actually I think ap tag inside of a list item is a no-no so I'll make this a span element and I may also want to add a break right there let's take a look at this so if we go back here reload get unexpected : and where does it say that this is at turn this mini and of course let's go back to customer we need that missing semicolon right there now if I reload you can see that we have Tony new MacBook new bike John has a new TV and then we see Mike and Mike does not have any orders so this is great because now we've just created the relationship from the customer to the orders and we can also create the inverse of that relationship to say that an order belongs to a customer so to do that I could go back here and I could say we have one-to-one one-to-many and I know that this is actually going to be the one to many but it's going to be the inverse of that so we have one too many inverse which is the belongs to so this is essentially what I want to have inside of my order model this is going to say that this order belongs to an app slash customer so now once I were to list out all the orders that I had let's run this again we'll say it orders equals app slash order all and maybe I'll just loop through each of these so I'll say for each orders as order so for every single order I'm just going to echo out a paragraph with the order name and then I'm going to say belongs to customer and I'll say order customer name and as long as they don't have any syntax errors here we should be able to go to our application test slash orders yep and you'll see here that we have each of these orders the new macbook order belongs to customer tony the new my bike belongs to customer tony and the new TV belongs to customer john so this is great because now we can create relationships between our tables and it just makes interacting with the data so much easier hey thanks for checking out this video series on laravel 7 basics i just wanted to say thanks for watching this series and i also wanted to ask you to give me a follow on twitter at TN y le eh i also wanted to take just a second and let you know about one of my courses that i created called SAS adventure and you can visit it at SAS adventure io and this is a 21 day program that will teach you how to create your own software as a service using the laravel framework so if you want to support me go ahead and check out my course at SAS adventure io and i will see you in the next video
Info
Channel: devdojo
Views: 2,640
Rating: undefined out of 5
Keywords:
Id: Yy_BPU-Pp_c
Channel Id: undefined
Length: 10min 47sec (647 seconds)
Published: Sun Apr 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.