United (and related) we stand, divided we fall.

In a real-world context, everything is connected; for example, a car has an owner, a book has an author (or maybe more than one), or an e-commerce order is related to one or more products that a customer (another relation!) has ordered.

Everything, actually, is related!

There are no differences in the application development world; usually, you create software to solve a real-world problem. The real world is made from related things, so you will probably have to define many relationships between your entities.

However, let’s be clear: I am not saying anything new. Just go to Wikipedia and search for entity-relationship model.

Usually, in your school books, you can find three fundamental types of relationships:

  • One-to-one: This is used to relate a single entity with another single entity (for example, a person and an identity document)
  • One-to-many: This is used to define a connection between an entity with more entities of the same type (for example, all the books of the same author)
  • Many-to-many: This is used to relate multiple entities with many other entities (for example, a book can be a part of more than one category, and one category can include more than one book)

Of course, web development makes no exceptions. Eloquent makes no exceptions.

Following the convention used in what you have seen until now, Eloquent has a great way to deal with relationships, the methods used to define them for your models, and the techniques you can adopt to work with them.

So, let’s explore what we are going to do in this tutorial.

First of all, we will deal with the basic relationship types we just saw. How does Eloquent handle them? You will discover the beauty of powerful methods such as hasMany or belongsTo. This time, there are no more snippets; we will follow the creation of our library management tool classes, defining every entity and every relationship.

After the basics, you will discover how to work with these relationships: how to query and use them in a comfortable and clean way. Also, we will see how to insert and delete related models in your database, or update the existing models.

Sometimes, working with many-to-many relationships will mean storing some data specific to that relationship. Eloquent has a very useful property named pivot that you can use to query the desired pivot table.

So, many things to look at this time! However, it is not over yet! Eloquent offers two other relationships that you can use: the has many through and polymorphic many to many relationships.

Alright, enough chit-chat! I am not going to spoil anything now. Follow the chapter, and you will fall in love with it.

Obviously, I will show you a real-world example for every concept. Come on, hero!

  • The trinity: one to one, one to many, and many to many
  • Querying-related models
  • Eager loading (and the N + 1 problem)
  • Inserting and updating related models
  • Accessing distant relationships
  • More power! Polymorphic relationships

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.