Hello Devs,

In this tutorial, we are going to learn about how to create laravel one to Many eloquent relationship.

Follow this step by step guide given below:




Step 1 :

 

Create Migration

php artisan make:model Employee -m

php artisan make:model Salary -m


Path:database\migrations\2014_10_12_000000_create_employee_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEmployeeTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('employee', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('salary_id');
            $table->integer('extra_salary_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('employee');
    }
}


Path:database\migrations\2014_10_12_000000_create_salary_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSalaryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('salary', function (Blueprint $table) {
            $table->id();
            $table->integer('amount');
            $table->date('payment_date');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('salary');
    }
}


Run this command:

php artisan migrate



Step 2 :


Create Model Relationship app\Employee.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = [
        'name','salary_id','extra_salary_id'
    ];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function salary()
    {
        return $this->belongsTo(Salary::class);
    }
}

app\salary.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Salary extends Model
{
   /**
    * Run the migrations.
    *
    * @return void
    */
    protected $fillable = ['amount','payment_date'];
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function employer()
    {
        return $this->hasMany(Employer::class);
    }
}



Step 3 : 


Insert Records

app\Http\Controllers\Employee.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function Employee()
    {   
        $employee = employee::find(1);
        $employee->name = "Test Name";
        $employee->salary_id ="1"
        $employee->extra_salary_id ="2"
        
        $employee = $employee->salary()->save($employee);
    }
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function Salary()
    {
        $employee = employee::find(1);

        $salary1 = new Salary;
        $salary->amount = '123456789';
        $salary->payment_date = '15/07/2020';
        
        $salary2 = new Salary;
        $salary->amount = '123456789';
        $salary->payment_date = '16/07/2020';
        
        $employee = $employee->salary()->saveMany([$salary1, $salary2]);

    }
}



Step 4 : 


Retrieve Records app\Http\Controllers\Employee.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employee;
use App\Salary;
use Hash;

class EmployeeController extends Controller
{
    public function index()
    {
        $employee = Employee::find(1);
 
        $employee = $employee->salary;
 
        dd($employee);

        $salary = Salary::find(1);
 
        $salary = $salary->employee;
         
        dd($salary);
    }
}


I hope this example helps you.