laravel 5.6将表单数据作为路线参数传递

时间:2018-06-29 06:11:15

标签: php routing laravel-blade laravel-5.6

我刚刚开始学习laravel 5.6,即时通讯在将数据从表单传递到路由参数方面遇到问题。所以我有这条路线:

Route::get('/tours/index/{province_id?}', 'TourController@index');

这是我的表格:

<form method="GET" action="#">
                <div class="siem">
                    <i class="fas fa-compass"></i>
                    <input style="padding-left:5px" size="66" id="province_id" class="search" list="provinces" name="province_id" placeholder="Siem Reap, Sihanouk Ville...etc or click on location icon to let us locate you"
                    />
                    <datalist id="provinces">
                        @foreach($provinces as $province)
                            <option value="{{ $province->id }}">{{ $province->name }}</option>
                        @endforeach
                    </datalist>
                </div>
                <div class="tour">
                    <a onclick="searchProvince()" type="button" href="#">Find my tours</a>
                    <script>
                        function searchProvince(e){
                            // e.preventDefault();
                            let province_id = $("#province_id").val();
                            window.location.replace("/tours/index/" + province_id);
                        }
                    </script>
                    <i class="fas fa-search"></i>
                </div>
            </form>

现在,作为一种解决方法,我只是使用jquery来替换url,我知道这不是一个好习惯。我的TourController中具有以下索引方法:

public function index($province_id = null)
    {
            if($province_id != null){
                if(isset($_GET['sortBy']) == false){
                    $tours = Tour::with('latestTourImage')->where('province_id', $province_id)->get();
                    return view('tours.index', ['tours' => $tours, 'province_id' => $province_id, 'province' => Province::find($province_id)])->with('onGuest', '1');
                }
                else{
                    $tours = Tour::with('latestTourImage')->where('province_id', $province_id)->where('category', '=', $_GET['sortBy'])->get();
                    return view('tours.index', ['tours' => $tours, 'province_id' => $province_id, 'province' => Province::find($province_id)])->with('onGuest', '1');
                }
            }
            else{
                return redirect()->back();
            }

    }

0 个答案:

没有答案