Laravel Relationships Cheat Sheet

In this tutorial you will learn about the Laravel Relationships Cheat Sheet and its application with practical example.

In this Laravel Eloquent Relationships with example tutorial, I’ll show you how to create and use eloquent relationships in your laravel database model. In laravel , you can define Eloquent Relationships model classes. In eloquent model you can create custom relationship methods along with methods example hasOne(), hasMany(), belongsTo(), belongsToMany(), hasOneThrough() etc.

Eloquent Relationships in Laravel

In laravel eloquent model you are allowed to create following type for relationships.

Types of Eloquent Relationships in Laravel

  • One To One
  • One To Many
  • Many To Many
  • HasMany Through
  • Many To Many Polymorphic Relation

Laravel One to One Relationship with Example

In Laravel, one to one relationship is one of basic relationship. Suppose,  Post model would be associated with a content model using one to one relationship. This one to one relationship means every post can have one one content associated with it. To demonstrate this relationship, we will create a post_content() method within the Post model and call the hasOne() method to relate the Content model.

It is important to note that Eloquent establishes a foreign key based on the model name and should have a matching value id. Here post_id is the foreign_key of Content.

Laravel inverse of One to One Relationship Example

For now you are allowed to access the content from the post model. Now create an inverse relationship on the content model so that the post can accessed the content model . This can be achieved using laravel belongsTo method to get the post data from the content model.

you can access the post content using the relation method as following:

Laravel One to Many Relationship Example

In One of many relationships one single model can associated with multiple number of the other model. For example, an author may have many post.

you can now access the collection of post article by using the post() method as.Using this, we get all the posts of a single author. Here we need to get the author details of a single post,for each post there a single author, within the Post model includes belongsTo relation.

Inverse One to Many Relationship Example

The inverse of the one to many relationship is defined using laravel  belongsTo  method. Since we have access to post articles of an author, now we need to allow a post article to access its parent Author model or access author details by using the post model. This inverse relationship can be established using belongsTo method as following:

Now we can get the author details by using posts as following:

Laravel Many to Many Relationship Example

Let suppose now each of the post have multiple tags and each tags can belongs to multiple posts then this type of relationship is defined as many to many relationship. In laravel many to many relationship can be defined using belongsToMany() method.

Now you can access the tags in post model as following:

The inverse of Many to Many Relationship Example

The inverse of a many to many relationships can also be defined using belongsToMany method on the associated model as following:

you can now access the post in tag model as following:

Laravel HasMany Through Relationship

The “has-many-through” relationship allows you accessing distant relations via an intermediate model. For example, a Country model might have many Post models through an intermediate User model. In this example, you could easily gather all blog posts for a given country. Let’s look at the tables required to define this relationship:

In this tutorial we have learn about the Laravel Relationships Cheat Sheet and its application with practical example. I hope you will like this tutorial.