1

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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Does $id refer to the parameter?
I'm getting a Undefined variable: id
Yes it refers the parameter you define in route as {id}
Undefined variable: id <- I'm give you only the example. You have to get $id by yourself in the view partial :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.