Book Image

Learning Laravel's Eloquent

By : Francesco Malatesta
Book Image

Learning Laravel's Eloquent

By: Francesco Malatesta

Overview of this book

<p>Learning Laravel's Eloquent starts off by taking you through setting up your first project and guiding you in creating a perfect Laravel environment. You will learn how to build the right database structure with the Migrations system and the Schema Builder class. Next, you will be introduced to the main element of Eloquent: the model. After treating the model as a single, isolated entity, you will learn how to create relations between them. You will be taken through organizing, filtering, and sorting your data with collections. You will then learn to enhance an application with new features using events and by creating new observers. Towards the end of the book, you will discover how to install, configure, and use the Eloquent ORM without Laravel. The book concludes by walking you through how to deal with complex problems and build advanced and flexible systems.</p>
Table of Contents (16 chapters)
Learning Laravel's Eloquent
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

The trinity – one to one, one to many, many to many


As I mentioned earlier, we will start from the basics. So, the first thing we will see is how you can define relationships between entities in Eloquent. This is really simple, and usually you will just have to add a single line of code for every relationship.

One to one

Our library is very concerned about tracking people who borrow books. For this reason, every new user has to give to the library some identity document data.

Now, every user has a single identity document, and every document is absolutely unique. If you think about it, this is a perfect one-to-one relationship. When you build your database, the most followed rule tells you that you have to add the necessary columns in the first table. In this specific example, we would add columns to the user table.

However, someone could say "yes, but this is a completely different entity!"

Also, we could have to store many details about the identity document of every user: number, type, due...