Hi Dev,
In this tutorial, I Will explain you how to create laravel one to Many eloquent relationship. We will create one to many relationship in laravel. We will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc. I will show you laravel hasMany relationship example.
We will create "employee" and "salary" table.both table are connected with each other, I will create one to many 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 Many Relationship use "hasMany()" and "belongsTo()" for relation.
Here, I will give you full example for laravel one to many eloquent relationship as bellow.
Step:1 Create MigrationIn 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 hasMany 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->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'); } }Now run the below command to create employee and salary tables in your database:
php artisan migrateStep:2 Create Model Relationship
Now in this step, we have employee and Salary tables ready, let’s define the one to manMany relationship in our models. our app/Employee.php model file and add a new function called salary() which will return a hasMany 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','salary_id','extra_salary_id' ]; /** * Run the migrations. * * @return void */ public function salary() { return $this->belongsTo(Salary::class); } }
you can see, in the salary() method we are defining a hasMany relationship on Salary model.
Now we will defining Inverse One To Many Relationship in Laravel.As we have defined the hasMany 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 = ['amount','payment_date']; /** * Run the migrations. * * @return void */ public function employer() { return $this->hasMany(Employer::class); } }
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 RecordsLet’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 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
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); $employee = $employee->salary; dd($employee); $salary = Salary::find(1); $salary = $salary->employee; dd($salary); } }it will help you....