Book Image

Laravel 5 Essentials

By : Martin Bean
Book Image

Laravel 5 Essentials

By: Martin Bean

Overview of this book

Table of Contents (15 chapters)
Laravel 5 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Relationships


When we built our application in Chapter 3, Your First Application, we made use of relationships. Each cat in our application was of a particular breed. However, instead of storing the name of the breed next to every individual cat and potentially having the breed repeating numerous times, we created a separate breeds table and each cat's breed was a value that referred to the ID of a record in that table. This gave us an example of two types of relationships: a cat belongs to a breed, but a breed can have many cats. This is defined as a one-to-many relationship.

There are other types of relationships, for each of which Eloquent provides good support:

  • One-to-one

  • Many-to-many

  • Has-many-through

  • Polymorphic relations

  • Many-to-many polymorphic relations

Here, we will look through them with an example of each.

One-to-one

Sometimes, you may want to split data across multiple tables for ease of management, or because they represent two different parts of one entity. A common example is a user...