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.
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
ā Live Streamingā Interactive Chatā Private Showsā HD Quality
Anya is LIVE right now
FREE
Free to watch ⢠No registration required ⢠HD streaming
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).