2
votes

i have a problem when updating the database

error: Method Illuminate\Database\Eloquent\Collection::save does not exist.

controller:

 public function actualizar(Request $request){
    
    $nuevoIngreso = \App\Models\Servicio::all();
    $nuevoIngreso->costo = $request->costo;
    $nuevoIngreso->duracion = $request->duracion;
    $nuevoIngreso->mantenimiento = $request->mantenimiento;
    $nuevoIngreso->save();
    return redirect('servicios')->with('mensaje', 'Ficha Actualizada.');

}

blade.php

         <form method="POST" action="{{ route('actualizar') }}">
                {{method_field('PUT')}}
@csrf

                    @foreach ($servicios as $costo)
<h1 class="text-center text-primary">  {{$costo->tipo}} </h1>
                  <div class="form-group row">
                    <label for="example-text-input" class="col-md-2 col-form-label form-control-label">Costo</label>
                    <div class="col-md-10">
                      <input class="form-control" type="text" value="{{$costo->costo}}">
                
                    </div>
                    <label for="example-text-input" class="col-md-2 col-form-label form-control-label">DuraciĆ³n</label>
                    <div class="col-md-10">
                      <input class="form-control" type="text" value="{{$costo->duracion}}" id="example-text-input">
                    </div>
                    <label for="example-text-input" class="col-md-2 col-form-label form-control-label">Mantenimiento</label>
                    <div class="col-md-10">
                      <input class="form-control" type="text" value="{{$costo->mantenimiento}}" id="example-text-input">
                    </div>
                  </div>
                 <hr>
               @endforeach

               <button type="submit" class="btn btn-success float-right" float-rightdata-toggle="sweet-alert" data-sweet-alert="success">SAVE</button>
                </form>

help please

1

1 Answers

1
votes

Error is clear.you are calling static method all().So it should be

  $nuevoIngreso =new \App\Models\Servicio();
    $nuevoIngreso->costo = $request->costo;
    $nuevoIngreso->duracion = $request->duracion;
    $nuevoIngreso->mantenimiento = $request->mantenimiento;
    $nuevoIngreso->save();

So if you are thinking to update particular record then find by id or any column

 $nuevoIngreso =new \App\Models\Servicio::find($id);
        $nuevoIngreso->costo = $request->costo;
        $nuevoIngreso->duracion = $request->duracion;
        $nuevoIngreso->mantenimiento = $request->mantenimiento;
        $nuevoIngreso->save();

Updated

<form method="POST" action="{{ route('actualizar') }}">
    {{method_field('PUT')}}
    @csrf

    @foreach ($servicios as $key=>$costo)
        <h1 class="text-center text-primary">  {{$costo->tipo}} </h1>
        <input class="form-control" type="hidden" value="{{$costo->id}}" name="service[{{$key}}][id]">
        <div class="form-group row">
            <label for="example-text-input" class="col-md-2 col-form-label form-control-label">Costo</label>
            <div class="col-md-10">
                <input class="form-control" type="text" value="{{$costo->costo}}" name="service[{{$key}}][costo]">

            </div>
            <label for="example-text-input" class="col-md-2 col-form-label form-control-label">DuraciĆ³n</label>
            <div class="col-md-10">
                <input class="form-control" type="text" value="{{$costo->duracion}}" id="example-text-input" name="service[{{$key}}][duracion]">
            </div>
            <label for="example-text-input" class="col-md-2 col-form-label form-control-label">Mantenimiento</label>
            <div class="col-md-10">
                <input class="form-control" type="text" value="{{$costo->mantenimiento}}" id="example-text-input" name="service[{{$key}}][mantenimiento]">
            </div>
        </div>
        <hr>
    @endforeach

    <button type="submit" class="btn btn-success float-right" float-rightdata-toggle="sweet-alert" data-sweet-alert="success">SAVE</button>
</form>

Then in controller

foreach($request->service as $key=>$value){

 $nuevoIngreso =new \App\Models\Servicio::find($value['id']);
 if($nuevoIngreso){
 $nuevoIngreso->costo = $value['costo'];
 $nuevoIngreso->duracion = $value['duracion'];
 $nuevoIngreso->mantenimiento = $value['mantenimiento'];
 $nuevoIngreso->save();
 }
}