Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

423
Visualizações
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...

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

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

teachers

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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', [App\Http\Controllers\MyController::class, 'updateGraderStatus']);

Add the below code to where your checkbox is located

$('.toggle-class').on('change',function(){
    let status = $(this).prop('checked') == true ? true : false;
    let grader_id = $(this).data('id');
    $.ajax({
       type:'GET',
       dataType:'json',
       url:'/path/to/updateGrader/status', // add your earlier created route here
       data:{'grade': status, 'grader_id': grader_id},
       success: function(data){
        console.log('success');
     }
   });
});

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;
       $grader->save(); 
}

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda