我想按价格订购产品,一个链接从低到高,另一个从高到低, 当我从视图中单击链接时,它不会影响产品的顺序。 这是控制器中的功能:
public function products($category_url, Request $request){
Product::getProducts($category_url, self:: $data);
if ($category1 = Categorie::where('url', '=', $category_url)->first()) {
$sort = $request->get('sort', 'asc');
$products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
return view('content.products', self::$data , compact('products', 'sort'));
}
}
这是路线:
Route::get('shop/{category_url}?sort=DESC','ShopController@products');
Route::get('shop/{category_url}?sort=ASC','ShopController@products');
tihs是观点:
<a href="{{ url('shop/'.$category['url'])}}?sort=DESC" style="color:black"> High to low</a> |
<a href="{{ url('shop/'.$category['url'])}}?sort=ASC" style="color:black">Low to high</a>
答案 0 :(得分:0)
只需制作
之类的路线即可Route::get('shop/{category_url}','ShopController@products');
在班级中定义排序的默认值
static $sort= 'asc';
获取具有排序
的产品的示例代码class ShopController extends Controller{
static $sort = 'asc';
public function products($category_url, Request $request){
// Your Code
//check the value you are getting
//echo $request->input('sort');
$sort = !is_null($request->input('sort')) ? $request->input('sort') : static::$sort;
$products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
// Your Code
}
// Your Code
...
}
答案 1 :(得分:0)
你没有得到$ sort值。
执行:
dd($sort);