Level 73
@amrith I am curious to know what have you tried? Or you just want this to be resolved by someone so that you don't bother to learn how?
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I want to save this API output to 03 tables. Employee , Salaries,Designations how could i do it using single code line ?
{
"emp_no": 1,
"birth_date": "1981-11-26",
"first_name": "Abba",
"last_name": "Subba",
"gender": "m",
"hire_date": "2020-02-15",
"salaries": {
"salary": 90000,
"from_date": "2020-01-02",
"to_date": "2020-02-01"
},
"designations": {
"title": "Consultant",
"from_date": "2020-01-02",
"to_date": "2020-02-01"
}
}
Employee.php
<?php
class Employee extends Model
{
public function salaries(){
return $this->hasMany(Salary::class,'emp_no','emp_no');
}
public function designations(){
return $this->hasMany(Designation::class,'emp_no','emp_no');
}
}
Salary.php
class Salary extends Model
{
use HasFactory;
public function employee(){
return $this->belongsTo(Employee::class,'emp_no');
}
}
Designation.php
class Designation extends Model
{
.........
public function employee()
{
return $this->belongsTo(Designation::class,'emp_no');
}
.........
}
EmployeeController.php
public function store(EmployeeRequest $request)
{
$data = $request->validated();
$emp = Employee::create($data);
$emp->load('salaries')->save($data['salaries']);
$designations = $data['designations'];
$emp->load('designations')->save($data['salaries']);
}
@Amrith so instead of load() try:
$emp->salaries()->save($data['salaries']);
// and
$emp->designations()->save($data['designations']);
Please or to participate in this conversation.