Skip to content

Laravel 7 – Update value of checkbox without form or button

I’m stuck with my project, please help…

This is the situation: In my project I have to assign a teacher to give permission to grade students, because a group can have 2 or more teachers, but only one has to have permission to grade, so… I added a column in my table ‘teachers’ in the database with the name ‘grade’ of type boolean. And in my view I added a checkbox… How do I change the value of a checkbox without using any form or button to trigger the action and activate the checkbox in the teacher who is going to have permission to grade? And this value should also be updated in the database.

  • This is my view…
    <table class=" table table-sm table-striped table-bordered ">
        <thead class="text-white bg-primary">
        <tbody class="small text-center">
            @foreach ($group->teachers as $teacher)
                    <td >{{$teacher->Identifier}} </td>
                    <td >{{$teacher->email}} </td>
                        <input id="active" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade" value="1">
                        <form method="post" action="{{url('Groups/Disenroll/'.$group->idGroup.'?idTeacher='.$teacher->idTeacher)}}">
                            <button type="submit"  class="btn btn-sm btn-outline-danger"  > <i class="material-icons small" >call_split</i></button>

And this is an image of the table, if it helps…




I think you can use something like JQuery for this.

You can firstly add a class of toggle-class to your checkbox

<input class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">

And secondly, make sure that you have a way to keep track of who’s doing the grades.

Add data-id=”{{ $teacher->Identifier}}” on your checkbox

<input data-id="{{ $teacher->Identifier}}" class="active toggle-class" {{$teacher->grade == 1 ?'checked':''}} type="checkbox" name="grade">

In your web.php add a new route

Route::get('/path/to/updateGrader/status', [AppHttpControllersMyController::class, 'updateGraderStatus']);

Add the below code to where your checkbox is located

    let status = $(this).prop('checked') == true ? true : false;
    let grader_id = $(this).data('id');
       url:'/path/to/updateGrader/status', // add your earlier created route here
       data:{'grade': status, 'grader_id': grader_id},
       success: function(data){

In your Controller, MyController in our case create a new function

public function updateGraderStatus(Request $request)
       $grader = MyModel::find($request->grader_id);
       $grader->grade = $request->grade;

Please let me know if this helps you. I didn’t test it out but I think this is how you would go about it.