Delete dynamic form data from database laravel

Viewed 28

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, enter image description here

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

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.

1 Answers

With possible future developments, the thing you try to have to do will evolve into something too complex. If I understood correctly, you are using a frontend framework like Vue, React, etc. In this situation, the best option is to use multiple save buttons for each education entry.

When someone clicks the save button, you will check the related education entry for having an id attribute on the frontend side. If it already has an id attribute, you can make an HTTP request to the update endpoint. Otherwise, you will make an HTTP request to the create endpoint, grab the id attribute from the response, and assign it to the education entry on the frontend side.

Related