박보근
박보근

Reputation: 115

laravel route name parameter

Use a pageNation to request the value of the page to the controller. But why can't any parameters reach the controller?

 Route::get('/index', 'Penpal\ViewController@index')->name('penpal.index');

 <form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="get">
            <select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
                    <option value="3">@lang('penpal/component/indexMenu.twelve')</option>
                    <option value="4">@lang('penpal/component/indexMenu.twenty_four')</option>
                    <option value="5">@lang('penpal/component/indexMenu.thirty_six')</option>
                </select>
        </form>    


 public function index (Request $request){
    return $request;\
 }

A parameter named "menu" cannot be received from the controller.

Upvotes: 0

Views: 462

Answers (3)

Aditya Thakur
Aditya Thakur

Reputation: 2610

You haven't set any route parameters for your route and neither passing any to your controller method. And it would be a better idea to use POST then GET.

Change this to

Route::get('/index', 'Penpal\ViewController@index')->name('penpal.index');

this

Route::post('/index/{menu?}', 'Penpal\ViewController@index')->name('penpal.index');

and your form

<form action="{{ route('penpal.index', ['menu' => 'p11-c3']) }}" method="POST">
@csrf

And in your controller method you can fetch the passed parameter

 public function index (Request $request, $menu){
    print_r($menu);
 }

Upvotes: 0

Ahmed Abdullah
Ahmed Abdullah

Reputation: 41

Use Post method both route and form

<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="post">
Route::match(['get','post'],'/index', 'Penpal\ViewController@index')->name('penpal.index');

Upvotes: 0

thisiskelvin
thisiskelvin

Reputation: 4202

Your <form> is using method='get', instead of method='POST' (which is used to post data to the request via a form.

You will also need to use @csrf in your blade template or you will not be able to post data:

<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="POST">
   @csrf
   <select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
      <option value="3">@lang('penpal/component/indexMenu.twelve')</option>
      <option value="4">@lang('penpal/component/indexMenu.twenty_four')</option>
      <option value="5">@lang('penpal/component/indexMenu.thirty_six')</option>
   </select>
</form>

Finally, make sure that your route is a ::post() route.

Upvotes: 3

Related Questions