Laravel 8 Has Many Through Eloquent Relationship Tutorial

Laravel eloquent relationship is a very important feature which connects one or more tables in a chain. This is the substitute of joins in laravel.

Laravel provides these following relationships –

Eloquent relationships are defined as methods on your Eloquent model classes. Inside this article we will see the concept of laravel 8 Has Many through Eloquent relationship.

This article will give you the detailed concept of about implementation of Has Many through relationship in laravel.

For this tutorial we will consider a people table, brokers table and homes table. This means a person will contact a broker, then broker will provide home list.

Person -> Broker -> Home

Let’s get started.

Table of Contents

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Create Migrations

We need few migration files –

  • People migration to store people data
  • Broker migration to store brokers
  • Home Migration to store homes data

Open project into terminal and run these artisan commands.

$ php artisan make:migration create_people_table$ php artisan make:migration create_brokers_table$ php artisan make:migration create_homes_table

Read More[…]

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors