For getting every new tutorial link Please join our telegram group

Part 2 laravel eloquent one to many relationship using hasMany() method in model

In this laravel eloquent relationship tutorial we will discuss these problems. How to use one to many relationship, laravel one to many relationship with hindi video tutorial, one to many relationship not working, hasMay relationship in laravel.

Laravel eloquent one to many relationship with example

Laravel one to many relationship use when a table one row related to other table many rows means one row many relations in second table. This relationship return multiple rows of related table with main table one row. hasMany() method use in eloquent model for defining many to many relationship. hasMany() relationship method first argument pass related model class name.

Let an example of hasMany() relationship. An employee get salary every month so employee related to many row in salary table. simply a parent model have many child data here Employee is parent model and Salary is child model.

For getting all salary data related to employee with Employee model define a hasMany() relationship in Employee model.

employees table : id, name, age, created_at, updated_at

salarys table : id, employee_id, amount, created_at, updated_at

Employee Model
<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model {
    
    public function salary() {
        return $this->hasMany(Salary::class);
    }
}
EmployeeController

this is our EmployeeController where we get relationship data using with() method

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Employee;

class EmployeeController extends Controller{
    public function index(){
        $employees = Employee::with('salary')->get();

        foreach($employees as $employee){
	    foreach($employee->salary as $salary){
	        dd($salary->amount); //print salary amount
	    }
	}

	or print all employees data
	dd($employees);
    }
}

Customize hasMany() method

When our table primary key or foreign key column name not follow standard way then customize relationship second and third argument. standared way as primary key name have 'id' and foreign key name table singular form underscore id like salary_id.

 return $this->hasMany(Salary::class, 'foreign_key', 'local_key');

hasMany inverse relationship

hasMany inverse relationship same work as hasOne inverse relationship. When we get relationship table data and want to get main table data with it then call inverse relationship and its value get by belongsTo() method. In our example when get Salary model data and also take employee name with it than use a employee method that are define in Salary model with belongsTo() relationship.

Salary model example for hasMany inverse relationship
<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Salary extends Model {
    
    public function employee() {
        return $this->belongsTo(Employee::class);
    }
}

Customize belongsTo() method

When we customize foreign key and primary key name

 return $this->belongsTo(Employee::class, 'foreign_key', 'owner_key');
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links