Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to Toggle checkbox and update the checkbox field in the database

Hello everyone.

I am developing a project in Laravel-5.8. In the project I need to set the current appraisal using a checkbox. The checkbox field is is_current and is tinyint (boolean).

By default, it is zero(0). If the checkbox is checked from the view as current, in the database it should turn the field is_current of that particular row to 1. Then all other is_current in the database should be set to zero(0). And also save it in the database.

Controller

public function create()
{
    abort_unless(\Gate::allows('appraisal_identity_create'), 403);
    return view('appraisal.appraisal_identities.create');
}

public function store(StoreAppraisalIdentityRequest $request)
{
    abort_unless(\Gate::allows('identity_create'), 403);

    $appraisalStart = Carbon::parse($request->appraisal_start);
    $appraisalEnd = Carbon::parse($request->appraisal_end);
    $submissionStart = Carbon::parse($request->submission_start);
    $submissionEnd = Carbon::parse($request->submission_end);
    
    $identity = AppraisalIdentity::create([
        'appraisal_name'                 => $request->appraisal_name,
        'appraisal_start'                => $appraisalStart,
        'appraisal_end'                  => $appraisalEnd,
        'submission_start'               => $submissionStart,
        'submission_end'                 => $submissionEnd,
        'is_current'                     => $request->is_current,
        'is_appraisal_end'               => $request->is_appraisal_end,
        'company_id'                     => Auth::user()->company_id,
        'created_by'                     => Auth::user()->id,
        'created_at'                     => date("Y-m-d H:i:s"),
        'is_active'                      => 1,
    ]);
    Session::flash('success', 'Appraisal is created successfully');
    return redirect()->route('appraisal.appraisal_identities.index');
}

View

   <form  action="{{route('appraisal.appraisal_identities.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
                    <div class="form-body">
                        <div class="row">
            <div class="col-md-6">
                                <div class="form-group row">
                                    <label class="control-label text-right col-md-3">Is Current Appraisal?</label>
                <div class="col-md-9">
                                        <input type="checkbox" class="form-control"  name="is_current" value="{{old('is_current')}}">
                                    </div>
                                </div>
            </div>
                        </div>
                    </div>
      
        <div>
            <!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
            &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
            <button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
        </div>
    </form>

How do I adjust my controller and view to achieve this?

Thank you.

0 likes
3 replies
jlrdw's avatar

If a checkbox is not checked nothing is passed.

So in your request there will be only checked. If a single check box just check if it has that request otherwise you have to Loop through and get the checked data.

if ($request->has('whatever')) {
    //
}

Fyi dealing with check boxes has been thoroughly covered many times in the forum.

noblemfd's avatar

How do I go about that. Can you give me a sample?

Please or to participate in this conversation.