Hello Devs,

In this tutorial, we are going to learn how to create laravel one to one 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->string('phone');
            $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('salary');
            $table->string('employee_id');
            $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','phone'];

    /**
    * Run the migrations.
    *
    * @return void
    */
    public function salary()
    {
       return $this->hasOne('App\Salary');
    }
}

app\salary.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Salary extends Model
{
   /**
     * Run the migrations.
     *
     * @return void
     */
     protected $fillable = ['salary','employee_id'];

    /**
    * Run the migrations.
    *
    * @return void
    */
    public function employee()
    {
       return $this->belongsTo('App\Employee');
    }
}



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 salary()
    {
        $employee = new Employee;
        $employee->name = "Test Name";
        $employee->phone = "100000";
        $employee->save();

        $salary = new Salary;
        $salary->salary = '123456789';
        $salary->employee_id = '1';
        $employee->salary()->save($salary);
    }
}



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);

       // get employee data from Salary model
       $employee = Salary::find(1)->employee;
       dd($employee);

       // get Salary number from employee model
       $salary = employee::find(1)->salary;
       dd($salary);
    }


I hope this example helps you.