doutanghuan9595 2016-10-06 14:38
浏览 40
已采纳

Laravel 5.2将params添加到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.

  • 写回答

1条回答 默认 最新

  • douren7179 2016-10-06 16:10
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊