Laravel One to One Eloquent Relationship Example

Laravel 6 , Laravel , Laravel 7

Nicesnippets

1204

02-06-2020


Laravel One to One Eloquent Relationship Example

Hi Guys,

In this tutorial, I Will explain how to create laravel one to one eloquent relationship.We will show one to one model relationship in laravel. you will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc. we will show laravel hasone relationship example.

In this example , we will create "employee" and "salary" table.both table are connected with each other, i will create one to one relationship with each other by using laravel Eloquent Model, We will first create database migration, then model

retrieve records and then how to create records too.One to One Relationship will use "hasOne()" and "belongsTo()" for relation.

Here the example of laravel one to one eloquent relationship.

Step:1 Create Migration

In this example, we will need two tables in our database employee and salary.I will start to create the model and migrations, then we will define the one to manOne relationship.To generate models and migrations, run below two commands in your terminal.

php artisan make:model Employee -m

php artisan make:model Salary -m

Above command will generate two models in your app folder called Employee and Salary. You will also find two new migrations in your database/migrations folder.

Your newly created models and migration after add table field:

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

}

}

Now run the below command to create employee and salary tables in your database:

php artisan migrate

Step:2 Create Model Relationship

Now in this step, we have employee and Salary tables ready, let’s define the one to manOne relationship in our models.

our app/Employee.php model file and add a new function called salary() which will return a hasOne relationship like below:

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

}

}

you can see, in the salary() method we are defining a hasOne relationship on Salary model.

Now we will defining Inverse One To Many Relationship in Laravel.As we have defined the hasOne relationship on Brand model which return the related records of Salary model, we can define the inverse relationship on the Salary model.Open your app/Salary.php model file and add a new method called employee() in it which will return the related brand of a 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');

}

}

As you can see, in employee() method we are returning a belongs to relation which will return the related employee of the salary.

Step 3 : Insert Records

Let’s in this step add data to employee and salary from controller:

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

Now in this step retrieve records model

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

}

it will help you....


Recommended Posts