Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Laravel 5.x Cookbook

You're reading from   Laravel 5.x Cookbook A recipe-based book to help you efficiently create amazing PHP-based applications with Laravel 5.x

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781786462084
Length 402 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Terry Matula Terry Matula
Author Profile Icon Terry Matula
Terry Matula
Alfred Nutile Alfred Nutile
Author Profile Icon Alfred Nutile
Alfred Nutile
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Setting Up and Installing Laravel FREE CHAPTER 2. Using Composer Packages 3. Routing 4. Building Views and Adding Style 5. Working with Data 6. Adding Angular to Your App 7. Authentication, Security, and Subscriptions 8. Testing and Debugging Your Application 9. Adding Advanced Features to Your App 10. Deploying Your App Index

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…

  1. Run this inside the virtual machine console:

    >php artisan make:migration create_favorites_table
    
  2. Open this file, and add the database/migrations/2016_05_16_220136_create_favorites_table.php fields:

  3. Then, make a model for this file:

    >php artisan make:model Favorite
    
  4. In this file, we can add a relationship back to the user:

  5. Then, open the user file called app/User.php to relate back to the favourites:

  6. 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image