Mamulasa Mamulasa - 1 year ago 264
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')) !!}

And this Controller:

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

$restaurants = User::with(['articles' => function ($q){

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

$restaurants = $restaurants->get();

return view('pages.menues')->withRestaurants($restaurants)

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download