Skip to content
Advertisement

My laravel application is not picking right route

My laravel application should move to route “mango/public/4” but it moves to “mango/public/4/4”. Route file

route::get('/{id}/edit','LaptopController@edit');
route::patch('/{id}','LaptopController@update');

Update(view)

<form method="post" action="{{$laptop->id}}">
    @csrf
    @method('PATCH')
   <table width="100%">
       <tr>
           <td>Enter Name</td>
           <td><input type="text"  name="name"></td>
       </tr>

       <tr>
           <td>Enter Description</td>
           <td><input type="text"  name="description"></td>
       </tr>

       <tr>
           <td><button>Update</button></td>

       </tr>


   </table>
</form>

LaptopController

public function edit($id){
        $laptop=Laptop::find($id);
        return view('update',['laptop'=>$laptop]);
    }

Advertisement

Answer

You can do it easy if you name your route When your action is just an id for example action="4", it means that form will be sent to relative path, and that’s why you have /4/4 in your url

// web.php
...
route::patch('/{id}','LaptopController@update')->name('laptop.update');

// update view
<form method="post" action="{{ route('laptop.update', ['id' => $laptop->id]) }}">
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement