Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:12Length:2.1 hours
  • Overview
  • Transcript

4.1 Setting Up Relationships

Our example uses the most common type of relationship: a one-to-many relationship. I'll show you how to redesign the tables, set up the model, and reseed the database, with this relationship.

4.1 Setting Up Relationships

Whenever you are working with a relational database, chances are very good that you're going to have multiple tables that have relationships to one another. Now that's not always the case because there's always going to be a few tables that are standalone tables where they have no relationship. But a relational database exists for creating relationships between tables. And the example that we've been working with through this course are guitars database, is no exception. We have two tables, manufacturers and guitars. And there should be a relationship between these two tables because a manufacturer makes guitars. So we should be able to refer to a given manufacturers' guitars and visa versa. And the way that we do that is through a relationship. So I've used the term primary key throughout this course. The primary key is how you identify a given record within a table. So the id within the manufacturers the primary key for the manufacturers table. Now, if you know SQL, I apologize for explaining this, but if you don't know SQL I am going to throw some terms at you that you might not know. So, primary key is the ID for a table. The primary key for the manufacturers table is the id. So what we want to do is set up what's called a foreign key inside of the guitars table. A foreign key is the ID that is used inside of another table. So, we're going to set up a foreign key inside of the guitars table that points to the primary key of the manufacturers table. We're going to be setting up what's called a one too many relationship, and there are many different types of relationships. There's a one to one, where one record in one table matches another record in another table, and there's just one match there. But then there's a one-to-many where a single record in one table matches multiple records in another table. That's the relationship that we are going to work with and that's probably the most common relationship. But then there's a many to many relationship, where many records in one table has a match to many records in other table. That's a little bit more complex. And there are time when you would have a many-to-many but we're going to be working with a one-to-many. So when it comes to setting up a relationship, not just with Eloquence but really any ORM we essentially need to do two things. We need to set up our database so that it is properly set up for relationships. I mentioned that we're going to add a foreign key to our guitars table, so we need to modify our guitars table. And then after we set up our database we will then tell Eloquent that we have a relationship between the manufacturer model, and the guitar model. So the first thing we're going to do is just blow away everything that we have done as far as the database is concerned. So we're going to say php migrate:reset, and that's going to roll back everything. So whenever we look at the database now, the only table we have is migrations and nothing else. And that's what we want to start with. And let's go to our database folder, and let's go to the migration's folder and let's get rid of the final migration that we did. Where we added the year to the guitar's table because we're going to add that year whenever we create the guitar's table to begin with, so we don't need that anymore. So let's go ahead and add that. We'll say table integer and we will say that that is the year, but then we also need to set up that foreign key to the manufacturers' table. So a foreign key has a naming convention, and it's not just for Eloquence or MySQL, but what you would typically see is a name like this. You would start with the singular name of the table that we're setting up a relationship with, so that would be manufacturer_id. So that way, whenever you are looking at the table itself, you can see that this is the manufacturer ID and then you can take that value and look up the manufacturer by that ID. So we're going to add that, but I also want to add this. In the previous lesson, I talked about soft delete. Well, we can set up soft deletes, that's a little difficult to say with our migrations. All we have to do is use this method inside of our migration where we say softDeletes. That's actually there's a little bit more that we have to do here, or at least not here, but inside of our model. But this is going to set up the ability so that we soft delete records as opposed to hard deleting them, which is actually deleting the actual records from the database. With a soft delete there's a flag in the database and if the flag is set or not set determines if that record is deleted or not. So we're going to add this soft deletes to the manufacturers table as well so let's go ahead and do that. Now we don't have to set up anything as far as a foreign key inside of the manufacturers' table. Because this is a one-to-many relationship, so one manufacturer is going to have many guitars so there's no need to have a foreign key here inside of manufacturers. So with our database schema all ready to go we can close those files and we'll go to our models. So we'll start with our Manufacturer model. We want to set up a relationship between our Manufacturer model and the Guitar model. And we do that with a function called guitars, and we could call this anything that we want, but since we are working with guitars, it makes sense to call it Guitars. And we're going to do this, we will say return $this hasMany. So this is essentially saying that this manufacturer model has many relationships, or many matches, with our Guitar model. And that's all that we need to do there. Well, at least as far as setting up this particular relationship. We are going to pull in some other things so we can set up soft deletes. So we are going to set up a use statement. Illuminate\Database\Eloquent\SoftDeletes. And we want our model class to use the soft deletes' trait, so we'll say use softDeletes so that it will pull in the methods for soft deleting. Then we're going to say protected $dates, and deleted_at is the column name that's going to be added to our table. And this essentially says that the column deleted_at should be mutated into a date, that's all that we are doing here. And we need to do this same thing inside of our Guitar model, and we're going to do a few other things to the Guitar model. But for right now we're just going to set up the soft deletes, so let's do that. And just a simply copying and pasting will get us started there. Now, we need to adjust the way that we seed our database. Because we want to properly set up the relationships between the records within our guitars and the records within our manufacturers. So let's go to our database folder once again, and let's go to seeds, and let's go the GuitarsTableSeeder. Now, we're going to adjust this class here, and we're only going to use this class to seed our database. We're going to set up our tables and everything inside of our GuitarsTableSeeder. So, what we could do is go ahead and comment this out so that code is not going to execute and we of course want to truncate our table. So let's do that for manufacturer as well as guitar so truncate. And then we want to create our record so we're going to start with our manufacturer record. And we're going to do this, we're going to say $prs = App\Manufacturer, and we're going to use the create method. And all we need to do is set the name for our manufacturers. So in this case, name is going to be PRS. Now we're saving these to a variable so that we can reuse them later to create the records for our individual guitars. It's not actually necessary, but it's going to make things just a little bit easier to read. And since this is quite a bit of typing, I'm going to copy and paste the remainder of the manufacturers. So we have we have Fender and we have Gibson, and, of course, their names correspond with their variable names. So, now we want to create our guitar records themselves, and, we have done that here with our existing code, but we also want to sent up the relationship between a guitar and its manufacturer. Now, technically, we could do this, whenever we call the creates method and we pass in the name and the year we could come in here we can say, manufacture id. And then set that to the appropriate id. But we don't want to do that, we want to let Eloquent to set up this relationship for us. That's the whole point of using NORM, so that all of the messyness, and all of this stuff is being done behind the scenes so that we don't have to worry about it. Eloquent gives us an easy way of setting up a relationship, and creating records that are related to one another. So we might as well rely upon Eloquent to do what we are wanting it to do. Now we can create related records in a variety of different ways, and I'm going to show you pretty much all of them. So the first thing we're going to do is create our guitars made by Paul Reed-Smith, PRS. So we're going to say $prs guitars, this is the guitars method that we wrote inside of our manufacturer model, it's that one right there. So we say guitars and if we wanted to, we could say get, and that would get the guitars. This is a query builder or at least it's part of the query builder. So we are returning the query builder, and if we call get, that's going to retrieve the guitars. However, there is no guitars, so there is no sense in calling get right now. But we do have a method called save. And the save method accepts a model, since we have a guitar model that is the related model to our manufacturer we just create that model. So we say new App\Guitar, we pass in the data that we want that guitar to have, and there we go. Now we do not say create, that is going to actually create that record in the database, we don't want that. We are going to let Eloquent do that for us by creating the related records for us by calling save, so this is one way of doing it. Now we have two guitars that are made by PRS, so there is actually a better way of doing it this way. But, let's copy that code anyway, because we're gonna use that as a basis for our other method. So we have save, we also have saveMany. So those times when you have one record and multiple related records, the saveMany is going to work. Now we essentially pass the same thing, but we pass an array of model objects. So in this case, the first element in this array is going to be the Vela. The second is going to be the Starla. So let's add that here as well, so new App\Guitar then we pass in the Starla information. So that's the second way, we have save and saveMany, the only difference is that saveMany accepts an array of model objects. The third way Is a method called create, so we're going to use our fender here. We're gonna say $guitars and we're going to call create. Create doesn't accept a model object, it simply accepts the data that we want that model object to have. So we are going to use the Strat information here. We pass in an array that has the name and the year, and that's all that we have to do here, so that's the third method. And then the fourth is you've probably already guessed, create many, so we have guitars and then createMany. It's kind of a combination between the saveMany and the create method, we parse in an array of arrays and those child arrays, I guess you could call them, contain the information for our records. So we have the Les Paul, and we have the Explore, and there we go. It's a lot cleaner at least to my opinion to use the create methods. But feel free to use the save method, if that's gonna float your boat. So let's get rid of the old create methods and let's also now use our factory here, we'll just stick with our five hard-coded there or guitars. So let's go ahead and reset up our database. So we'll say php artisan migrate:refresh, I knew it's re-something and seed, and that should give us everything. So if we look at our database now, let's refresh the page. We will have our tables, and the manufacturers table really hasn't changed at all, except for that deleted_at, we can see that here as well. We can see that it is null, and whenever we actually delete a record, well then there's going to be a date there. But if we look at guitars, we have the id, we have the name, we have the year and then we have the manufacturer_id. So for the Paul Reed Smith guitars, the Vela and the Starla, we see that the manufacturer id is 1, for the Strat, the id is 2, that was Fender. And then for the Gibson guitars, the Les Paul and the Explorer, the manufacturer ID is three. So that relationship was already created for us, we don't have to do anything else. And so now that we have our database ready to go in the next lesson were going to just play around. You'll learn how to get the related records from a manufacturer and we'll also setup the ability to get the manufacturer from a guitar. Eloquent makes it very easy to do.

Back to the top