0

I have a checkbox field in my form, I want to insert data to database if the checkbox is checked and delete the data in database if the checkbox is unchecked.

Here is my checkbox code:

<ul class="list-unstyled mb-0">
   @foreach ($companies as $company)
   <li class="d-inline-block mr-2 mb-1">
      <fieldset>
         <div class="checkbox">
            <input type="checkbox" name="company_id[]"
            class="checkbox-input" value="{{ $company->id }}"
            id="{{ $company->id }}" 
            @foreach ($supervisor_company as $spv)
              @if ($spv != null)
                @if ($spv->company_id == $company->id)
                  checked
                @endif
              @endif
            @endforeach>
            <label for="{{ $company->id }}">{{ $company->name }}</label>
         </div>
      </fieldset>
   </li>
   <li class="d-inline-block mr-2 mb-1">
      @endforeach
</ul>

And this is my controller:

if ($request->has('company_id')) {
    foreach ($request->company_id as $key => $company) {
        $spv_data = EmployeeSpvCompany::where('employee_id', $employee_id)->where('company_id', $company)->first();
        if ($spv_data != null) {
            EmployeeSpvCompany::where('employee_id', $employee_id)->where('company_id', $company)->update(['company_id' => $company]);
        } else {
            $emp_spv = new EmployeeSpvCompany;
            $emp_spv->employee_id = $employee_id;
            $emp_spv->company_id = $company;
            $emp_spv->save();
        }
    }
}

Insert to database if the checkbox is checked is already working, but I don't know how to delete the data in database if the checkbox is unchecked

4
  • you would need to know the records you passed to the view to then compare to the list you get back ... also, in your controller it looks like you are attempting to recreate the updateOrCreate method (assuming you are only expecting to be updating 1 record) but you are not actually updating any field Commented Jan 6, 2022 at 4:06
  • @lagbox the update method in my code still not working. tbh, I'm trying to delete record not updating record. so...don't mind the update method Commented Jan 6, 2022 at 4:15
  • but in your code, you were trying to update the record instead of delete! or I miss something. Commented Jan 6, 2022 at 4:19
  • @SachinKumar Like I said, I don't have any idea how to do that. I'm just try anything that come to my head, so don't mind the update method Commented Jan 6, 2022 at 4:21

2 Answers 2

1

I expect the company_id field is a multi-select type so the value will be returned in the form of an array. I guess the following code will work in this situation.

$companyIds = $request->has('company_id'); // [1, 2]

if ($companyIds) {
    foreach ($companyIds as $company_id) {
        $employeeSpvCompanyInstance = EmployeeSpvCompany::firstOrCreate([
            'employee_id' => $employee_id,
            'company_id' => $company_id
        ]);
    }
    EmployeeSpvCompany::where('employee_id', $employee_id)->whereNotIn('company_id', $companyIds)->delete();
} else {
    EmployeeSpvCompany::where('employee_id', $employee_id)->delete();
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is more simple & specific than my code. I'm changing my if block to firstOrCreate. Thanks for your answer
Happy coding buddy!
1

Ah!! I found something called whereNotIn. I just need to check if the data is not in my array request.

EmployeeSpvCompany::whereNotIn('company_id', $request->company_id)->delete();

1 Comment

I was writing my answer to my editor. But yes the same concept I wrote in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.