skip to Main Content

i have an input called id and i want to specify the button path for that id example:

in my blade file

<div class="modal-body">
      <lable for="">ID:</lable><br/>
    <input type="text" name="id"  placeholder="Enter the product name" required><br/>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" onclick="location.href='/produtos/editar/{{$id}}';" class="btn btn-primary" >Save changes</button>
      </div>

Now my controler.php

public function update(Request $request, $id){
    $produto=Produto::findOrFail($id);
    $produto->update([
        'id'=>$request->id,
    'Nome'=>$request->nome,
    'custo'=>$request->custo,
    'preco'=>$request->preco,
    'quantidade'=>$request->quantidade,
    'Marca'=>$request->marcas,
      'Voltagem'=>$request->Voltagem,
      'Descricao'=>$request->Descricao,
    ]);
    return " Produto Atualizado com Sucesso!";
}

now in routes file

Route::get('/produtos/editar/{id}', 'AppHttpControllersProdutosController@edit');
Route::post('/produtos/editar/{id}', 'AppHttpControllersProdutosController@update')->name('alterar_produto');

Being able to get the input value and put it along the route to the file

2

Answers


  1. You need to set post method for update data. You have onclick action on submit button, but your route method is POST. Therefore, your update procedure doesn’t work. You need to change code with element, like that:

    <form action="/produtos/editar/<?=$id?>" method="post">
        <div class="modal-body">
            <lable for="">ID:</lable>
            <br/>
            <input type="text" name="id" placeholder="Enter the product name" required><br/>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </form>
    
    Login or Signup to reply.
  2. You need to use laravel form for that

    {{ Form::open(array('action' => array('AppHttpControllersProdutosController@update', $produto->id), 'method' => 'POST')) }}
    
      <div class="modal-body">
       <lable for="">ID:</lable><br/>
       <input type="text" name="id"  placeholder="Enter the product name" required> 
       <br/>
      </div>
      <div class="modal-footer">
       <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
       <button type="submit" class="btn btn-primary" >Save changes</button>
      </div>
    
     {{ Form::close() }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search