Php laravel 5.6将表单数据作为路线参数传递
我刚刚开始学习Laravel5.6,我在将数据从表单传递到路由参数时遇到了一个问题。所以我有这个路线:Php laravel 5.6将表单数据作为路线参数传递,php,routing,laravel-blade,laravel-5.6,Php,Routing,Laravel Blade,Laravel 5.6,我刚刚开始学习Laravel5.6,我在将数据从表单传递到路由参数时遇到了一个问题。所以我有这个路线: Route::get('/tours/index/{province_id?}', 'TourController@index'); 这是我的表格: <form method="GET" action="#"> <div class="siem"> <i class="fas fa-comp
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>
为什么要在路由文件中打问号?在传递参数时?@hariGS,因为我认为在某些情况下它可能是可选的。问题是url中没有显示这个
{province\u id?}
。@hungrykoala问题是我不知道如何从表单向控制器发送数据,然后重定向到该路由。现在,我只能在url栏中手动输入省\号。@AcousticMike只需选中它即可删除问号并检查其工作情况。
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();
}
}