Search

Laravel One To Many - HasMany Eloquent Relationship (ORM)Tutorial

  • Share this:
post-title

In a relational database management system, you know that database tables are often related to one another. For example, a country may have many states could be related to the country who state it country wise. Laravel eloquent makes managing and working with these relationships very convenient. Laravel provides many relationships and in this tutorial, we will see how we handle one to many or we can call this hasMany eloquent relationship.

What is One To Many relationship ?

A one-to-many relationship is a very basic type of database relationship. When one table refers to many rows in another table that is called a one-to-many relationship. like country model is collection of countries and satate model is collection of cites and depend on state id as forign key

one to many - hasMany()

lets start one to hasMany relationship in laravel step wise

we have already setup a laravel new project if you want to use same project the view or read previously tutorial and review that link below. after all setup laravel project.

Click Here ?

Eloquent relationships are defined as methods in your Eloquent we will place a country method on the State model. We can use the hasMany() method to define one to hasMany relationships and the first argument passed to the hasMany method is the country id of the related model class.

Step 1: Create a state methods on the Countries model

Syntax :

// $this->hasMany(Model::class,'foreign_key','local_key');  
// remember, foreign_key & local_key are optional

app/Models/Countries.php

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
 
class State extends Model
{
    /**
     * Get the state data associated with the country.
     */
    public function state()
    {
        return $this->hasMany(State::class,'id','country_id');
    }
}

Step 2: Create CountryController

Make a file in app/Http/Controllers/CountryController.php and add the given code.

app/Http/Controllers/CountryController.php

<?php

namespace App\Http\Controllers;

use App\Models\Countries;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function index($id)
    {   
        $result = Countries::find($id);
        /**
         * Get the state associated with the country.
         */
        var_dump($result->state);
    }
}

Why to use with in laravel ?

with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.

Eloquent relationships fetch data using with() function

<?php

namespace App\Http\Controllers;

use App\Models\Countries;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function index($id)
    {   
        $state = State::with('state)->where(id,$id)->first();
        /**
         * Get the state associated with the country.
         */
        var_dump($state->state);
    }
}