EducationalDetailsController.php
public function update(EducationalDetailsRequest $request)
{
$educationDetails = $request->validated();
foreach ($educationDetails['educational_details'] as $education) {
$educationDetail = $this->educationalDetailsManager->find($education['education_id']);
$educationDetail->update(['education_level' => $education['education_level'],
'passed_year' => $education['passed_year'], 'institution' => $education['institution']]);
}
}
EducationalDetailManager.php
public function find(int $education_id){
return $this->educationalDetails->find($education_id);
}
EducationalDetailRequest
<?php
namespace App\Talent\EducationalDetails\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EducationalDetailsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'employee_id'=>'required|exists:employees,id',
'educational_details' => ['required', 'array'],
'educational_details.*.education_id'=>'required',
'educational_details.*.education_level' => 'required|string',
'educational_details.*.passed_year' => 'required|date_format:Y|before_or_equal:' . now()->format('Y'),
'educational_details.*.institution' => 'required|string',
];
}
}
EducationDetails.php
<?php
namespace App\Talent\EducationalDetails\Models;
use App\Talent\Employee\Model\Employee;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class EducationalDetails extends Model
{
use HasFactory;
protected $fillable = [
'employee_id',
'education_level',
'passed_year',
'institution'
];
public function employee(){
return $this->belongsTo(Employee::class);
}
}
This is my get API data for the edit form,

Here I am trying to create an update API of the educational details form which is dynamic.

Here update code is working fine that is whenever I edit form contents and hit save and continue the data present in the database gets updated only when those data are present in the table. But I also want to add new data when new data is added into the form and delete data whenever the user clicks the remove button in the form and hits save and continue. I am pretty new to laravel and I have no idea how can I implement that any help or suggestion will be really appreciated.