In this episode, we are starting out by reviewing what we have learned so far to create a company model and migration. For new content, we are tackling connecting the company to a customer using the belongsTo and hasMany eloquent relationship.
Each business has multiple lessons that they offer. This is called a has-many relationship. A piano instructor might have only “beginner” and “advanced” lessons, while another business may offer dozens of different lessons. The key concept here is that the concepts of business and lesson are related, and thus the models will be related. One business’s lessons should be independent of another business’s lessons, even though there is only one lessons table.
First I created the a migration to make the table: “php artisan make:migration create_lessons_table”. Then I edited the migration to add columns for business_id, name, and capacity. I also designated the business_id column to have an index. So this was the content of the up() in the migration:
The business_id is a foreign key to the businesses table. There is not, however, a foreign key constraint here. For now the foreign key will be enforced by the application logic. The table was created by running “php artisan migrate”. A model was added with “php artisan make:model Lesson”. To tell Laravel that a business has many lessons, a function is added to the Business model class.
public function lessons()
{
return $this->hasMany(Lesson::class);
}
Similarly I added a function to the Lesson model class to declare the inverse relationship.
public function business()
{
return $this->belongsTo(Business::class);
}
To examine this in action I inserted some sample data into the lessons table. Then from “php artisan tinker” I ran commands like “App\Business::first()->lessons;”, which returned an array of lesson objects that belonged to the first business object. Basically, tinker is a REPL that allows you to interact with objects directly.
Finally, the business/show.blade.php view was altered to include the lessons.
<p>This business offers the following lessons:</p>
<ul>
@foreach($business->lessons as $lesson)
<li>{{$lesson->name}} (capacity: {{$lesson->capacity}})</li>
@endforeach
</ul>
Refreshing the business detail page showed that it now also displays some data from the lessons that belong to that business.
Ruby on Rails is a web application framework written in Ruby. We’re building up to making our own web apps by making some of the smaller cogs and wheels first. Today, we focused on writing associations for models.
Sarah and I spent a long time poring over our association syntax and trying to parse what was going on in each magical little word, from which Rails is able to infer so much. It was trickier than we initially thought but after some TA help and some Googling we came up with the following takeaways, based on the code that follows:
belongs_to: the current model (Student) relates to exactly one of these (a college), and the foreign key is in the current model’s table (students.college_id).
has_one: the current model (Student) relates to exactly one of these (a major), and IT (the major) has the foreign key in its corresponding table (majors.student_id).
has_many: the current model (Student) relates to many of these (courses), and THEY (the courses) have the foreign key in their corresponding table (courses.student_id).