Add grades to several laravel students

0

I have the following view that shows all the students of a subject and its corresponding notes, how can I do to edit said fields?

This is my driver who receives the id of the subject as such and calls the students with their grades with the relationship.

public function agregarNota($id) 
{
    $asignatura = Asignatura::find($id);
    $alumnos = Asignatura::find($id)->alumnos;
    $calificaciones = Asignatura::find($id)->calificaciones;

    return view('calificaciones.agregar')->with('alumnos',$alumnos)->with('asignatura',$asignatura)->with('calificaciones',$calificaciones);
}

I need help to edit those fields, I'm used to the normal CRUD that is by rows.

This is the view

<table class="table table-bordered table-responsive">
		<tr>
			<th width="120">Rut</th>
			<th>N1</th>
			<th>N2</th>
			<th>N3</th>
			<th>N4</th>
			<th>N5</th>
			<th>N6</th>
			<th>N7</th>
			<th>N8</th>
			<th>Promedio</th>
			<th>Examen</th>
			<th>Final</th>	
		</tr>
			@foreach ($calificaciones as $c)
				<tr>
					<td>{{$c->alumno->rut}}</td>
        			<td>{!! Form::text('n1', $c->n1, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n2', $c->n2, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n3', $c->n3, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n4', $c->n4, array('class' => 'form-control')) !!}</td>  
        			<td>{!! Form::text('n5', $c->n5, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n6', $c->n6, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n7', $c->n7, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('n8', $c->n8, array('class' => 'form-control')) !!}</td>	 
        			<td>{!! Form::text('promedio', $c->promedio, array('class' => 'form-control', 'readonly' => 'readonly')) !!}</td>
        			<td>{!! Form::text('examen', $c->examen, array('class' => 'form-control')) !!}</td>
        			<td>{!! Form::text('final', $c->final, array('class' => 'form-control', 'readonly' => 'readonly')) !!}</td>    				  
  				
				</tr>
			@endforeach

	</table>
    
asked by Edgardo Escobar 08.05.2017 в 22:36
source

1 answer

0

What you can do is an asynchronous call every time you want to modify a note. You can do it in several ways, in my case I will use axios . In the example that I put, I change the text from the collective laravel to input since I do not know how to pass javascript functions inside it.

VISTA

@foreach ($calificaciones as $c)
<tr>
    <td>{{$c->alumno->rut}}</td>

   <td><input type="text" id="n1[{{$loop->iteration}}]" 
   onblur="actualizar('n1[{{$loop->iteration}}]','n1',{{$asignatura->id}})" 
   value="{{$c->n2}}">
   .
   .
   .
@endforeach

What you do here is assign an id to all the notes and each iteration with $ loop- & gt ; iteration (something like this would be seen n1[0],n1[1],n1[2] ), this is done in order to know which note will be modified at the time of making the call.

Once this is done, the onblur javascript event is used every time a user modifies a note. and leave the input this is activated. Now we assign the function actualizar that will have as parameters the id of the input, the name of the column that is going to be modified, and the id of the row of those notes (in this case I put a subject to it since I do not know how you have the tables). Next the script:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
     <script>
            function actualizar(nota,nombre,id) {
             nota = document.getElementById(nota).value;

              axios.put('/asignatura/'+ id, {nombre: nota})
                .then(function (res) {
                 //si se quiere retornar algo
                })
                .catch(function (err) {
                 //si sucede algún error
                });

          }
        </script>

This is where the asynchronous call is made to your laravel route that updates the data, as not your routes I put it in asignatura/{id} .

ROUTE

Route::put('asignatura/{id}',function(Request $request, $id){
        Asignatura::findOrFail($id)->update($request->all());
    });

Here you create the route that will update the model.

MODEL

protected $fillable = [
       'n1','n2','n3', //...
    ];

In the asignatura model, the $fillable of all the note columns must be added to allow us to change them when editing them.

Note

If you have problems with the laravel csrf you should add the following:

<script>
    window.Laravel = {!! json_encode([
        'csrfToken' => csrf_token(),
    ]) !!};
</script>

and before making the call in axios:

axios.defaults.headers.common['X-CSRF-TOKEN'] = Laravel.csrfToken;
    
answered by 09.05.2017 в 05:28