My routes:
Route::group(['prefix' => 'product'], function () {
Route::get('{id}', 'ProductController@product')->where('id', '[0-9]+');
Route::post('{id}/add', 'ProductController@addToCart')->where('id', '[0-9]+');
});
From the product/{id} page i wan't to do a POST to product/{id]/add
But what is the best way to get the form action url?
Now i have:
<form method="POST" action="{{ Request::url() }}/add">
It works, but I don't like it... And there must be a beter way...
<form method="POST" action="{{ action('ProductController@addToCart') }}/add">
Given me an exception...
Missing required parameters for [Route: ] [URI: product/{id}/add]. (View: .../resources/views/product/product.blade.php)
asked Nov 23, 2016 at 8:58
yooouuri
2,69810 gold badges35 silver badges55 bronze badges
1 Answer 1
If you dislike that, you can use route naming:
Route::post('{id}/add', 'ProductController@addToCart')
->name('product.add')
->where('id', '[0-9]+');
and then:
<form method="POST" action="{{ route('product.add', $id) }}">
where $id is a id of a element to pass.
answered Nov 23, 2016 at 9:02
Filip Koblański
10k4 gold badges33 silver badges37 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
yooouuri
Does
$id refer to the parameter?yooouuri
I'm getting a
Undefined variable: idFilip Koblański
Yes it refers the parameter you define in route as
{id}Filip Koblański
Undefined variable: id <- I'm give you only the example. You have to get $id by yourself in the view partial :)lang-php