What is Database Relationship?

The backbone of any relational database is a database relationship. Once you have defined tables, you can relate the data held in different tables. In other words, database relationships are associations between tables that are created using join statements to fetch data. To achieve this one table uses a foreign key that references the primary key of another table.

Types of relationship

One-to-One

A record in Table-A relates to one matching record in Table-B, and each record in Table-B relates to one matching record in Table-A.

Each record in the Employee table is about one employee. That record relates to only one record in the Pay table. Each record in the Pay table relates only one record in the Employee table.

One-to-Many

A record in Table-A can have many matching records in Table-B, but a record in Table-B can have only one matching record in Table-A.

A one-to-many relationship is the most common relationship found between tables in a relational database. The Customer table holds a unique record for each customer. Each customer can place many orders. Many records in the Order table can relate to only one record in the Customers table.

Many-to-Many

In a many-to-many relationship, each record in both tables can relate to many records in the other table. These relationships require a third table, called an intermediary table.

The relationship between the Customer and Order table is one-to-many. But the relationship between the Order table and Product table is many-to-many. An order can contain multiple products, and a product could be linked to multiple orders as several customers might submit an order that contains the same products. This kind of relationship requires a minimum of three tables.

Now let's continue with our project from the previous part — Migration and data seeding to creating Relationship among Models. An Eloquent relationship is a very important feature in Laravel that allows you to relate the tables in a very easy manner.

Below is an ER-Diagram (An ER diagram shows the relationship among entity sets) of entities involved in our laravel-news project.

Based on the above E-R Diagram now let's start writing relationship methods in the model classes.

In our application, a category will have many posts.

The comment will be written by a user and it will be on a post.

A post will be written by an author, belongs to a category; might have many comments, images, videos, and tags.

Multiple posts might belong to a tag.

A user may write multiple posts or comments.

Here we are done with the relationship between models. Now let's test if the relationships are correct or not.

To do so let's create a ‘test’ route in routes\web.php

Now start your database server and run php artisan:serve command, it will start the PHP Development Server at http://127.0.0.1:8000

Now open postman or similar application and send a GET request to http://127.0.0.1:8000/test

If everything works fine then you will see something like this:

The source code is available at https://github.com/mdutt247/laravel-news for you to refer to.

To dig deeper please check Laravel Documentation.

Read the previous part, Migration and data seeding of this tutorial.

Read the next part, Creating and Consuming RESTful API in Laravel.

Connect: Twitter, GitHub, Linkedin, MDITech

You can support by buying a coffee ☕️ here https://www.buymeacoffee.com/mdutt

Software Developer @ mditech.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store