update several rows of the same table

0

I am trying to update several rows of a table, for now I have only managed to update the first row and the rest is not updated.

foreach ($request->recibido as $key => $value) {
        $cont = Contenido::where('id_envio','=',$id)->first();
        $data = array($cont->recibido = $value);
        $cont->update($data);
    }

I'm working with laravel 5.5, try changing the first one for get () but it tells me that the update method does not exist.

    
asked by Jose 12.06.2018 в 18:31
source

1 answer

0

What happens is that the method first returns only one element, instead get returns a collection of elements, which you have to iterate to access them.

In your case you should do something like this:

foreach ($request->recibido as $key => $value) {
    $conts = Contenido::where('id_envio','=',$id)->get();
    // Aquí iteras la colección de contenidos y realizas una acción para cada uno de ellos
    foreach($conts as $key => $cont) {
        $data = array($cont->recibido = $value);
        $cont->update($data);
    }
}
    
answered by 12.06.2018 / 18:58
source