How to update several laravel php records

0

Well I'm making an attendance list and I want to update all of the people at once. I spent all day without solving it.

Sight code

@extends('layouts.docen')
@section ('contenido')

    

<h3>Editar Asistencia</h3>
@if (count($errors)>0)
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                <li>{{$error}}</li>
                @endforeach
            </ul>
        </div>
        @endif
</div>

   {!! Form :: model ($ CourseProgrammed, ['method' = > 'PATCH', 'route' = >    ['assistance.update', $ Programmed Course-> idAsistance]]) !!}         {{Form :: token ()}}

    <div class="col-lg-12 col-md-12 col-xs-12">
        <div class="table-reponsive">
            <table class="table table-striped table-bordered table-condensed table-hover">
                <thead>
                    <th>Alumno</th>
                    <th>Asistencia</th>
                </thead>

                <tr>
    @foreach($asistenciass as $reg)
                    <td>{{$reg->nombres}} {{$reg->apellidos_pat}} {{$reg->apellidos_mat}}</td>
                    <td>
                        <input type="hidden" name="idAsistencia[]" value="{{$reg->idAsistencia}}">
                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 form-group">
                            <div class="form-group">
                                <select name="asistencia[]" class="form-control">
                                        <option value="A" >Asistió</option>
                                        <option value="T" >Tarde</option>
                                        <option value="F">Falta</option>
                                </select>
                                </div>
                        </div>
                    </td>
                </tr>
    @endforeach 
            </table>
        </div>
    </div>
    <div class="col-lg-12 col-md-4 col-sm-2 col-xs-12 form-group form-group">
        <button class="btn btn-primary" type="submit">Guardar</button>
        <button class="btn btn-warning" type="reset">Limpiar</button>

        {!!Form::close()!!}
    </div>
 @endsection

Controller

public function update(AlumnoFormRequest $request)
{
    foreach ($request->get('asistencia') as $key => $value) {
        $asistencia->idAsistencia = $request->get('idAsistencia')[$key];;
        $asistencia->asistencia = $value;
        $asistencia->idCursoProg = $request->get('idCursoProg')[$key];
        $asistencia->IdRegistro = $request->get('IdRegistro')[$key];
        $asistencia->update();
    }
    return Redirect::to('asistencia');
}

error that comes to me

    
asked by Anderson Jamanca 30.11.2017 в 06:21
source

1 answer

0

The problem is that you do not have an object to work on.

In the case of new registrations:

$asistencia = new Asistencia;

With existing records:

$asistencia = Asistencia::find($request->get('idAsistencia')[$key]);

And the controller will look like this:

public function update(AlumnoFormRequest $request)
{
    foreach ($request->get('asistencia') as $key => $value) {
        $asistencia = Asistencia::find($request->get('idAsistencia')[$key]);
        $asistencia->idAsistencia = $request->get('idAsistencia')[$key];
        $asistencia->asistencia = $value;
        $asistencia->idCursoProg = $request->get('idCursoProg')[$key];
        $asistencia->IdRegistro = $request->get('IdRegistro')[$key];
        $asistencia->update();
    }
    return Redirect::to('asistencia');
}

As it seems that your code to create new and update existing is the same, you can use findOrCreate() , which would leave the controller as follows:

public function update(AlumnoFormRequest $request)
    {
        foreach ($request->get('asistencia') as $key => $value) {
            $asistencia = Asistencia::findOrCreate($request->get('idAsistencia')[$key]);
            $asistencia->idAsistencia = $request->get('idAsistencia')[$key];
            $asistencia->asistencia = $value;
            $asistencia->idCursoProg = $request->get('idCursoProg')[$key];
            $asistencia->IdRegistro = $request->get('IdRegistro')[$key];
            $asistencia->update();
        }
        return Redirect::to('asistencia');
    }
    
answered by 30.11.2017 / 08:04
source