Laravel Eloquent Relationships Example

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

In this Laravel Eloquent Relationships example tutorial, I’ll show you how to create and use eloquent relationships in laravel database model. In this article we will learn to implement and use different types of eloquent model relation available in laravel. We will also learn to perform create, insert and to delete operations in different type of relationships.

Type of Laravel Eloquent Relationships

In laravel, there are following types of eloquent relationships available:

Laravel One to One Relationship Example

The one to one relationship is a very common relationship in laravel model. In this example we will learn to implement and use one to one relationship in laravel. We will also show you how to  insert, retrieve, update, and delete data with the eloquent model from the database table in laravel.

Laravel Eloquent One to One Relationship Example

Let suppose we have two tables posts and contents in our example tutorial. In laravel we can create both tables using migration as following:

Post table migration:

Contents table migration:

In this example, the Post model is closely associated with a Content model in one to one relationship. In one to one relationship we will create a post_content() method within the Post model and there is hasOne() method to relate it with Content model as following.

The Eloquent model adds a foreign key based on the model name and should have a matching id value in it. In this relationship the post_id is the foreign_key for Content model,

Inverse of One to One Relationship Example

For now we can have the access to the content from the post model. Now we create an inverse relationship in the content model. For this we will use the belongsTo method for getting the post data from the content model as following.

We can now access the post content using the relation method as following:

Laravel One to Many Relationship Example

In this step by step tutorial I’ll guide you through to create many to many relationship and learn how to use this relationship. In this example we will use one to many relationship, and perform crud (create, read, update, delete) operation with the eloquent model from the database table in laravel.

Laravel Eloquent One to Many Relationship Example

Let suppose we have  tables name posts and authors. Using laravel migration, you can create both tables with the following fields:

Author table migration:

Post table migration:

Define One To Many Relationship

Now we will define One To Many Relationship for the model we have created. In One to many relationships means a relationship where one single model can owns the number of the other model. As per the example model we created, a single author can have written many post articles. Let’s take an example of one to many relationships.

Now, we can access the collection of post articles by using the post() method as.

Defining Inverse One To Many Relationship in Laravel

As of now we can access all the post articles of an author, now suppose if we want to allow a post model to access its Author model or access author details by using the post model. The inverse relationship of both One to One and One to Many works the same way. The inverse of the one to many relationship can be defined using the belongsTo method. Thus we can define in example mode as following:

Now we can get the author details by using posts in one to many relationship as following:

Retrieving Data In One to many relationship

In one to many relationship you can retrieve data from model as following:

You can get all posts of a particular author:

Similarly, you can retrieve the inverse related model.

Laravel Many to Many Relationship Example

In laravel using belongsToMany() method, you can define many to many relationship in laravel eloquent models. Then you can insert, update and delete data from table using many to many relationship in laravel.

Laravel Eloquent Many to Many Relationship Example

In this example we will demonstrate the implementation of Many to Many Relationship using two tables posts and tags. In this relationship each of the post can have many tags and each of the tag can be associated with many posts.

Now, lets understand how to define many to many relationships, Using belongsToMany() method:

Now, you  can access the tags in the post model as follow:

The inverse of Many to Many Relationship Example

In Laravel, the inverse relationship of a many to many relationships can be defined using belongsToMany method on the reverse model.

Now you can access the post in the tag model as follow:

Laravel Eloquent HasMany Through Relationship Example

In this Laravel Has Many Through Eloquent Relationship tutorial we will implement an example for the Laravel Has Many Through Eloquent Relationship. We will generate migration files of “users”, “posts” and “countries” table and then add a foreign key with users and posts table as following:

In users migration file:

In posts migration file:

In countries migration file:

In laravel, the “has-many-through” relationship provides a shortcut for accessing distant relations via an intermediate model relation. For example, a Country model might have many Post models through an intermediate User model. In this example,  we can easily get all blog posts of a given country. I’ll show you how to define HasMany Through relationship:

To fetch data using this relationship as follow:

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