Laravel 7 One to Many Eloquent Relationship Tutorial

Hi Artisan

In this tutorial i will discuss about laravel eloquent relationships example from scratch. There are many relationship in Laravel. But in this tutorial i will discuss about laravel hasmany through that mean one to many eloquent relationship example.

 If you don't know how to use laravel one to many relationship laravel 7 then this tutorial is for you. So let's start hasmany laravel 7 tutorial.

In this tutorial we are going to discuss about hasMany eloquent relationship example. For example, a company may have many customer, or an order could be related to the user who placed it.

Eloquent makes managing and working with these relationships easy, and supports several different types of relationships: some of them are

  1. One To One
  2. One To Many
  3. Many To Many
  4. Has One Through
  5. Has Many Through
  6. One To One (Polymorphic)
  7. One To Many (Polymorphic)
  8. Many To Many (Polymorphic)

Eloquent relationships are defined as methods on your Eloquent model classes. In this tutorial we are going to learn about Laravel hasmany eloquent relationship.

laravel-hasmany-eloquent-relationship

HasMany Eloquent Relationship Example

HasMany() relationship is a very basic relation. For example a company model might be associated with many customer. and a customer model might be associated with many company. Hope you will understand its relationship.

Let’s start code to better unserstand laravel hasmany eloquent relationship.So first create a fresh laravel app to do it.

Create-Project

Run this command to download a fresh laravel app

composer create-project --prefer-dist laravel/laravel eloquent

Create Database Migration

Read also : How to import and export csv file in laravel ?

We need two table for hasmany eloquent relationship example. One customer and other is compnay . so run two below commands to create migration 

php artisan make:model Company -m
php artisan make:model Customer -m

here -m for creating migration to corresponding model . Hope you understand. after running those command we have two table in our database/migrations folder. So open this two table and paste below code to two table.

Company table

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });
}

And this is our customer table

public function up()
{
    Schema::create('customers', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email');
        $table->integer('company_id');
        $table->timestamps();
        $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
    });
}

Our table is completed. Run below command to migrate those table into our database.

php artisan migrate

Now time to setup our relationship between company and customer. Se mind that one company might have many customer. So go to your company model and paste the below code.

namespace App;
use Illuminate\Database\Eloquent\Model;
 
class Company extends Model
{
    public function customers(){
   	return $this->hasMany(Customer::class);
 
   }
 
}

And we will create another relationship to customer to company. So paste below code to your customer model .

namespace App;
use Illuminate\Database\Eloquent\Model;
 
class Customer extends Model
{
    public function company()
    {
    	return $this->belongsTo(Company::class);
    }
}

Create Controller

Now we have to create our controller. To create controller , run following command

php artisan make:controller EloquentController

After creating controller. Now paste this following code to your EloquentController.php

app/Http/Controllers/EloquentController.php

namespace App\Http\Controllers;

use App\Company;
use App\Customer;
use Illuminate\Http\Request;
class EloquentController extends Controller
{
    public function Home()
    {
    	$companies = Company::all();
        return view('welcome',compact('companies'));
    }
}

Now time to setup our route. to create route just paste the following code.

Route::get('/','[email protected]');

Setup Blade file

Now for showing our relational data from database, Just paste the following code to welcome.blade.php.

Read also : Laravel 6 | Polymorphic Relationship Example Tutorial

Hope it can help you.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website