Help Update Laravel

Asked

Viewed 729 times

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: inserir a descrição da imagem aqui 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');
}

1 answer

3


No need to pass the id in the verb post, you just need to create a input hidden and save value id in this field, but, if you still want the way you did you need to pass the tag form the id because the address will really give 404 (because the address placed does not exist, it needs the code at the end, equal is stipulated in its route), change

<form action="/produtos/atualiza" method="post">

for

<form action="/produtos/atualiza/{$p->id}" method="post">

this change is upon your route, but if I were you would do it correctly so:

Route:

Route::get('/produtos/edita/{id}', 'ProdutoController@edita')->where('id', '[0-9]+');
Route::post('/produtos/atualiza', 'ProdutoController@atualiza');

Html:

@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);
    $produto->save();
    return redirect()->action('ProdutoController@lista');
}

References:

  • 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

  • 1

    @John I apologize, failed to remove the $id of the parameter should now work

  • 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.

  • 1

    @John you changed the form to is sending the value correctly, in which method is the error now? explains well the problem!

  • 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

  • Pay attention to my update and your update has difference ! @João

  • 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.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.