0
Good evening, I’m starting to study Laravel and I came across a problem that has already taken me a few hours in search of a solution. I need to update a form, and when I give the Submit I get the error: Just follow my codes: Route:
Route::get('/produtos/edita/{id}', 'ProdutoController@edita')->where('id', '[0-9]+');
Route::post('/produtos/atualiza', 'ProdutoController@atualiza');
Blade:
@extends('layout.principal')
@section('conteudo')
<h1>Editar Produto: {{$p->nome}}</h1>
<form action="/produtos/atualiza" method="post">
<input type="hidden" name="id" value="{{$p->id}}">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<div class="form-group">
<label>Nome</label>
<input name="nome" class="form-control" value="{{$p->nome}}">
</div>
<div class="form-group">
<label>Descrição</label>
<input name="descricao" class="form-control" value="{{$p->descricao}}">
</div>
<div class="form-group">
<label>Valor</label>
<input name="valor" class="form-control" value="{{$p->valor}}">
</div>
<div class="form-group">
<label>Quantidade</label>
<input type="number" name="quantidade" class="form-control" value="{{$p->quantidade}}">
</div>
<button type="submit" class="btn btn-primary btn-block">Adicionar</button>
</form>
@stop
Controller:
public function edita($id)
{
$produto = Produto::find($id);
return view('produto.editar')->with('p', $produto);
}
public function atualiza()
{
$params = Request::all();
$produto = Produto::find($params->id);
$produto->fill($params->except('id'));
$produto->save();
return redirect()->action('ProdutoController@lista');
}
Doing the second way it presented, I get the error "Too few Arguments to Function stock Http Controllers Productocontroller::updates(). 0 passed and Exactly 1 expected". From what I understand it is not able to receive the form id
– João
@John I apologize, failed to remove the
$id
of the parameter should now work– novic
i had already tested removing it, then I get the error in the Product::find line, where the error is : "Trying to get Property 'id' of non-object". I’ve been trying to solve this for a few days now, I’ve even managed to do a few tricks or ways that I don’t think are the ideas, but I’d like to keep it as close to ideal as possible.
– João
@John you changed the form to is sending the value correctly, in which method is the error now? explains well the problem!
– novic
I updated the post with an error photo, and updated my code to the current way following your recommendations. See if you can understand, anything at all
– João
Pay attention to my update and your update has difference ! @João
– novic
I had forgotten to update the controller part here in the post, now this identical, I actually came to give crt + c and crt + v in your code in mine. If it won’t take you much longer, I can move up my entire project for you to look at.
– João