我有一个过滤器路线,可以按品牌、重量、数量和口味过滤我的产品,一切正常,但是当我想添加排序过滤器时,它不起作用,我想按 pirce(升序和降序)对其进行排序. 这是我的代码 路线:
Route::match(['get', 'post'], 'products/filter', [SearchController::class, 'filterall']);
SearchController:
public function filterall(Request $request){
$data = $request->all();
// echo "<pre>"; print_r($data);
$brandUrl = "";
$weightUrl = "";
$countUrl = "";
$tasteUrl = "";
$sortUrl = "";
if(!empty($data['brandFilter'])){
foreach ($data['brandFilter'] as $brand){
if(empty($brandUrl)){
$brandUrl = "&brand=".$brand;
}else{
$brandUrl .= "-".$brand;
}
}
}
if(!empty($data['weightFilter'])){
foreach ($data['weightFilter'] as $weight){
if(empty($weightUrl)){
$weightUrl = "&weight=".$weight;
}else{
$weightUrl .= "-".$weight;
}
}
}
if(!empty($data['countFilter'])){
foreach ($data['countFilter'] as $count){
if(empty($countUrl)){
$countUrl = "&count=".$count;
}else{
$countUrl .= "-".$count;
}
}
}
if(!empty($data['tasteFilter'])){
foreach ($data['tasteFilter'] as $taste){
if(empty($tasteUrl)){
$tasteUrl = "&taste=".$taste;
}else{
$tasteUrl .= "-".$taste;
}
}
}
if(!empty($data['sort'])){
foreach ($data['sort'] as $sort){
if(empty($sortUrl)){
$sortUrl = "&sort=".$sort;
}else{
$sortUrl .= "-".$sort;
}
}
}
$finalUrl = "category/".$data['url_id'].'/'.$data['url_slug']."?".$brandUrl.$weightUrl.$countUrl.$tasteUrl.$sortUrl;
return redirect::to($finalUrl);
}
*** 我的视图控制器 ***
public function allCategory(Request $request, $id){
$catt = DB::table('categories')->where('id', $id)->first();
$products = Product::where('category_id', $catt->id)->where('status', '1')
->orderBy('brand_id', 'ASC');
if(!empty($_GET['brand'])){
$brandArray = explode('-', $_GET['brand']);
$products = $products->whereIn('brand_id', $brandArray);
}
if(!empty($_GET['weight'])){
$weightArray = explode('-', $_GET['weight']);
$products = $products->whereIn('product_weight', $weightArray);
}
if(!empty($_GET['count'])){
$countArray = explode('-', $_GET['count']);
$products = $products->whereIn('product_count', $countArray);
}
if(!empty($_GET['taste'])){
$tasteArray = explode(',', $_GET['taste']);
foreach ($tasteArray as $taste){
$products = $products->where('product_taste','LIKE', "%$taste%");
}
if(!empty($_GET['sort'])){
$sortArray = explode('-', $_GET['sort']);
$products = $products->orderBy('selling_price', $sortArray);
}
}
$products = $products->paginate(12);
return view('pages.all_category', compact('products', 'catt'));
}
这是视图代码:
<div class="row" id="search-results">
<div class="col-lg-12 col-md-12">
<div class="toolbar toolbar-products">
<div class="toolbar-sorter sorter">
<label class="sorter-label" for="sort">Sort By</label>
@if(!empty($_GET['sort']))
<?php $sortArray = explode('-', $_GET['sort']) ?>
@endif
<select id="sort" name="sort[]" class="sorter-options" onchange="javascript:this.form.submit();">
<option value="">Select</option>
@if(!empty($_GET['sort']))
<?php $price = $_GET['sort'] ?>
@endif
<option value="ASC" @if(!empty($sortArray) && in_array('ASC', $sortArray)) selected="" @endif>Lowest Price</option>
<option value="DESC" @if(!empty($sortArray) && in_array('DESC', $sortArray)) selected="" @endif>Highest Price</option>
</select>
</div>
</div>
</div>
</div>
我是通过页面中的表单来完成的。 我认为前面的 orderBy 控制器不起作用还有其他方法可以排序吗?