Php 按价格在laravel发布订购产品
我想按价格订购产品,一个链接从低到高,另一个链接从高到低, 但在我点击“从低到高”或“从高到低”后,顺序没有改变,它保持在同一页面上,但url正在改变 这是控制器中的功能:Php 按价格在laravel发布订购产品,php,laravel,laravel-5,laravel-5.2,laravel-5.3,Php,Laravel,Laravel 5,Laravel 5.2,Laravel 5.3,我想按价格订购产品,一个链接从低到高,另一个链接从高到低, 但在我点击“从低到高”或“从高到低”后,顺序没有改变,它保持在同一页面上,但url正在改变 这是控制器中的功能: public function products(Request $request, $category_url, $sort= 'ASC') { Product::getProducts($category_url, self:: $data); if ( $category1 = Categorie::
public function products(Request $request, $category_url, $sort= 'ASC')
{
Product::getProducts($category_url, self:: $data);
if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) {
$products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
return view('content.products', self::$data , compact('products', 'sort'));
}
}
if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) {
$products = Product::where('categorie_id', $category1->getAttribute('id'));
$products = strtolower($sort) == 'asc' ? $products->orderBy('price'):$products->orderByDesc('price');
$products = $products->get();
return view('content.products', self::$data , compact('products', 'sort'));
}
这是路线:
Route::get('shop/{category_url}/sorting-{sort?}', 'ShopController@products');
这些是来自视图的链接,视图是content.products
|
模型:
class Product extends Model {
static public function getProducts($category_url, &$data){
$data['products']=$data['category']=[];
if ($category=Categorie::where('url','=', $category_url)->first()){
$category= $category->toArray();
$data['category']=$category;
$data['title']=$data['title']. ' | ' . $category['title'];
if ($products=Categorie::find( $category['id'])->products){
$data['products']= $products->toArray();
}
}
}
在控制器中尝试以下操作:
public function products(Request $request, $category_url, $sort= 'ASC')
{
Product::getProducts($category_url, self:: $data);
if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) {
$products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
return view('content.products', self::$data , compact('products', 'sort'));
}
}
if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) {
$products = Product::where('categorie_id', $category1->getAttribute('id'));
$products = strtolower($sort) == 'asc' ? $products->orderBy('price'):$products->orderByDesc('price');
$products = $products->get();
return view('content.products', self::$data , compact('products', 'sort'));
}
将其更改为此,但订单仍然没有更改,产品保持相同的订单,因此问题在于您的查询尝试检查查询。我检查了查询,得到:“选择*自
产品其中分类id
=?按价格描述订购”问题在分类id中?