Mamulasa Mamulasa - 2 months ago 12
PHP Question

Laravel 5.2 add params to url

I need to pass a value from a form(dropdwon) and append this value to a url.

My Routes are:

Route::get('menues/{city?}', 'PagesController@menue');
Route::post('/', 'PagesController@menue');


I have a simple form:

{!!Form::open(array('action' => 'PagesController@menue', 'method' => 'POST'))!!}
{!! Form::select('city', array('heilbronn' => 'Heilbronn', 'stuttgart' => 'Stuttgart')) !!}
{!!Form::submit('Senden')!!}
{!!Form::close()!!}


And this Controller:

public function menue(Request $request, $city = null) {
$searchinput = $request->input('city');

$restaurants = User::with(['articles' => function ($q){
$q->nowpublished();
}]);

if(!is_null($city) && !is_null(User::where('city', $city)->first())) {
$restaurants->where('city', '=', $city);
}

$restaurants = $restaurants->get();

return view('pages.menues')->withRestaurants($restaurants)
//->withArticles($articles)
->withCity($city)
->withSearchinput($searchinput);
}


I need to append the value ($searchinput) from the previous page to show only entries for a particular city.

Answer

You need to change your form method attribute to GET, then in your controller you can do follow:

public function menue(Request $request) {
  $city = $request->city;

  // bla-bla-bla

  if ( ! is_null($city) &&  && ! is_null(User::where('city', $city)->first()) {
    // bla-bla
  }

  // return
}