Creating a relationship with favorites
This is an Angular section, but I will take a moment to create a simple relationship, which we will later build a widget around. In this case, I am going to add a favorites
table to the system and relate this to a user. It will hold the information I need to show a comic from Maravel.
Getting ready
A base installation of Laravel with the User model migrated is required here.
How to do it…
Run this inside the virtual machine console:
>php artisan make:migration create_favorites_table
Open this file, and add the
database/migrations/2016_05_16_220136_create_favorites_table.php
fields:Then, make a model for this file:
>php artisan make:model Favorite
In this file, we can add a relationship back to the user:
Then, open the user file called
app/User.php
to relate back to thefavourites
:Now, we are ready to build this UI.
How it works…
This is a fairly normal migration with a relationship with just a little extra. For one, notice the plural name of the favorites...