lundi 20 avril 2020

why the route declared in the two forms is the same while the url in the address bar is different?

here I have a example:

chrome url 1: https://trung.com/products/create

chrome url 2: https://trung.com/products/7/edit

Note: both the address is run on local

when I checked in Devtools, the form routes are the same, even though they differ in the url address bar.

Here is the form part in both the create page and the edit page in devtools , they are the same:

<form action="http://trung.com/products" method="POST" enctype="multipart/form-data"><input type="hidden" name="_token" value="FD8DM85jqVGyJ7f87sgNMV5Z0W4vq9Chq5nmncqe"> <input type="hidden" name="_method" value="PATCH">

//elements in the form

</form>

Here is the code followed:

route/web.php

Route::get('products','ProductsController@index')->name('products.index');

Route::get('products/create','ProductsController@create')->name('products.create');

Route::post('products','ProductsController@store')->name('products.store');

Route::get('products/{id}/show','ProductsController@show')->name('products.show');

Route::get('products/{id}/edit', 'ProductsController@edit')->name('products.edit');

Route::patch('products/{id}','ProductsController@update')->name('products.update');

Route::delete('products/{id}/delete','ProductsController@destroy')->name('products.delete');

ProductsController.php

public function create()
    {
      $category=categories::all();
      $product=new products();
      return view('products.create',compact('category','product'));
    }

    public function store(Request $req)
    {
        $validate=request()->validate([
            'product_name'=>'required|min:3',
            'amount'=>'required',
            'category_id'=>'required',
            'price'=>'required'
        ]);
       products::create($validate);
       return redirect()->route('products.index')->with('success','Add product successfull');
    }

    public function show($id)
    {
        return view('products.detail')->with('product',products::find($id));
    }

    public function edit(products $product)
    {
      $category=categories::all();
        return view('products.update',compact('category','product'));
    }

   public function update($id,Request $req)
   {
       $product=products::find($id)->update($req->all());
       return redirect()->route('products.index');
   }

view/products/index.blade.php

//open table
    <td>
                        <a href="" class="btn btn-sm btn-inline"><i
                                class="fas fa-eye"></i></a>

                        <a href="" class="btn btn-sm"><i
                                class="fas fa-pen-alt"></i></a>

                        <a href="" class="btn btn-sm"><i
                                class="fas fa-trash"></i></a>
                    </td>
//close table

view/products/create.blade.php

<form action="" method="post">
        @csrf
        @include('products.form')
        <button type="submit" class="btn btn-info">Add</button>
    </form>

view/products/update.blade.php

<form action="" method="POST" enctype="multipart/form-data">
        @method('PATCH')
        @include('products.form')
        <button type="submit" class="btn btn-success">update</button>
    </form>

thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire