Gamer Gamer - 6 months ago 12
PHP Question

Laravel 5.2 : Getting form fields in controller method

I have the following form :

{!! Form::open(['action' => 'PublicController@showProfile','method' => 'post', 'class' => 'form-horizontal']) !!}
{!! Form::token(); !!}
<input type="text" class="form-control" name="from">
<span class="input-group-addon"> to </span>
<input type="text" class="form-control" name="to">
<button type="submit" class="btn blue">Query</button>
{!! Form::close() !!}

In my controller I am getting the form fields like this :

public function showProfile(Request $request)
$to = $request->get("to");
$from = $request->get("from");
$giftReceived = App\GiftSent::whereBetween('created_at', [$from, $to])->get();
return view('user.profile',compact('giftReceived'));

In the above code

Am i missing something ?


First of all, as stated in your comments, you are using Route::get and submitting the form as POST which is obviously wrong. So, you should probably get MethodNotAllowedException

So, kindly change the line to

{!! Form::open(['action' => 'PublicController@showProfile','method' => 'GET', 'class' => 'form-horizontal']) !!}

In any case, instead of using Laravel's Request Class, prefer using the helper function request()

So, basically, it should be like

public function showProfile() {
  $from = request()->get('from');
  $to = request()->get('to');
  dd($from, $to);

Everything should work fine now :)