1

Cordial saludo. Tengo un problema a la hora de actualizar unos registro en la base de datos, al parecer los datos son tomados correctamente desde vue.

editarCat(item) {
            this.editarcat.Editarid_categoria=item.id_categoria;
            this.editarcat.Editarnombre_categoria=item.nombre_cat;
            console.log(this.editarcat.Editarid_categoria, this.editarcat.Editarnombre_categoria)
        }

Este es el código que utilizo para recoger los datos de un array que son recorridos por un V-For y meterlos en los inputs cuando presione el botón con el evento editarCat() para que se puedan editar desde los mismos inputs.

editarCategoria(){
             axios.put(`/categorias/${this.editarcat.Editarid_categoria}`, 
              this.editarcat.Editarnombre_categoria)
                .then(res=>{
                 console.log(res.data);
                })
        },

Aquí manejo la petición Axios y le paso los parámetros de Id y nombre_categoria

Route::resource('/categorias', 'categoriaController');

En el archivo web donde establecí las ruta para los distintos métodos HTTP

public function update(Request $request, $id)
    {
        $categoria = Categoria::whereRaw('id_categoria = ?', [$id])->get();
        $categoria->nombre_cat = $request->nombre_cat;
        $categoria->save();
        return $categoria;
    }

Cabe destacar que para buscar la categoría por un id no me funcionaba con el find() así que busque a ver como me podría funcionar y de esta forma por lo menos me retorna la tupla, también intente hacer el update con Query builder, pero tampoco funciono. Agradecería muchísimo que me pudieran ayudar a encontrar el error.

Tabla de la base de datos:

Schema::create('categorias', function (Blueprint $table) {
            $table->bigIncrements('id_categoria');
            $table->string('nombre_cat');
            $table->timestamps();
        });

//Mensajes de Log

[

2020-09-15 20:24:03] local.ERROR: Method Illuminate\Database\Eloquent\Collection::save does not exist. {"userId":2,"exception":"[object] (BadMethodCallException(code: 0): Method Illuminate\\Database\\Eloquent\\Collection::save does not exist. at C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\Traits\\Macroable.php:103)
[stacktrace]
#0 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\app\\Http\\Controllers\\categoriaController.php(88): Illuminate\\Support\\Collection->__call('save', Array)
#1 [internal function]: App\\Http\\Controllers\\categoriaController->update(Object(Illuminate\\Http\\Request), '1')
#2 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php(54): call_user_func_array(Array, Array)
#3 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('update', Array)
#4 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(239): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\categoriaController), 'update')
#5 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Route.php(196): Illuminate\\Routing\\Route->runController()
#6 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(685): Illuminate\\Routing\\Route->run()
#7 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))
#8 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Middleware\\SubstituteBindings.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#9 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#10 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken.php(76): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#11 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#12 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#13 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#14 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#15 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php(62): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest(Object(Illuminate\\Http\\Request), Object(Illuminate\\Session\\Store), Object(Closure))
#16 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Session\\Middleware\\StartSession->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#17 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#18 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#19 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php(66): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#20 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#21 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#22 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(687): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#23 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(662): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))
#24 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(628): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))
#25 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php(617): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))
#26 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(165): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))
#27 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))
#28 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#29 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#30 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#31 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#32 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#33 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#34 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php(63): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#35 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#36 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\fruitcake\\laravel-cors\\src\\HandleCors.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#37 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Fruitcake\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#38 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\fideloper\\proxy\\src\\TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#39 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))
#40 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))
#41 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(140): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))
#42 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(109): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))
#43 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\public\\index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#44 C:\\Users\\Deneros\\Desktop\\IOApp\\IOAppArgon Version 1.4\\server.php(21): require_once('C:\\\\Users\\\\Denero...')
#45 {main}
"} 
4
  • Hay mensajes de log que puedas agregar?
    – user187182
    Commented el 15 sept. 2020 a las 20:12
  • Buscare, gracias por la sugerencia
    – Acris
    Commented el 15 sept. 2020 a las 20:13
  • Ya añadí los mensajes
    – Acris
    Commented el 15 sept. 2020 a las 20:27
  • en la primera linea te está diciendo exactamente lo que tienes que mirar.
    – user187182
    Commented el 15 sept. 2020 a las 20:29

1 respuesta 1

0

La forma en que construyes la actualización no es correcta, prueba así:

  • Si usas get el resultado de vuelta sería una colección y tendrías que iterar para acceder al objeto deseado

  • No hace falta el uso de whereRaw pues tenemos métodos como findOrFail() los cuales nos permiten filtrar pasando como argumento el valor de la llave primaria

    • Para este punto estoy asumiendo que id_categoria es la llave primaria asociada al modelo Categoria
  • Aqui puedes leer mas al respecto sobre actualización

    public function update(Request $request, $id)
    {
        $categoria = Categoria::findOrFail($id);
        $categoria->nombre_cat = $request->nombre_cat;
        $categoria->save();
        return $categoria;
    }
    
7
  • Gracias por responder, lo intente con el findOrFail() y tampoco me sirvió, me sigue dando el mismo 500
    – Acris
    Commented el 15 sept. 2020 a las 20:47
  • Exactamente el mismo mensaje de error?
    – user128299
    Commented el 15 sept. 2020 a las 20:48
  • Efectivamente, el mismo error.
    – Acris
    Commented el 15 sept. 2020 a las 20:52
  • Cuando hago dd dentro del controlador me lanza un mensaje demasiado extenso, tal asi que no puedo pegarlo en la pregunta. No se si lo estaré haciendo mal, apenas inicie con php y vue hace poco
    – Acris
    Commented el 15 sept. 2020 a las 21:53
  • Trata con un request->all() y revisa si están llegando datos al método, no hace falta lo agregues a tu pregunta, solo revisa si algo llega
    – user128299
    Commented el 15 sept. 2020 a las 22:25

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.