繁体   English   中英

按价格订购产品

[英]Order products by price issue

我想按价格订购产品,其中一个链接从低到高,另一个从高到低,我收到此错误:“ im打开“时缺少App \\ Http \\ Controllers \\ ShopController :: products()的参数3” content.products”页面。 这是控制器中的功能:

public function products(Request $request,$category_url,$sort){
    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')); 
    }
}

这是路线:

  Route::get('shop/{category_url}/sorting-{sort}','ShopController@products');

这些是视图中的链接,视图是content.products

  <a href="  {{ url('shop/'.$category['url'].'/sorting-asc')}}" 
 style="color:black"> High to low</a> |
    <a href="  {{ url('shop/'.$category['url'].'/sorting-desc')}}" style="color:black">Low to high</a>

我认为,如果您将所有内容都保留为友好链接,则您的链接看起来会更加优雅。 因此,如果您愿意,可以使用

Route::get('shop/{category_url}/sorting-{sort}','ShopController@products');

并向您的函数添加参数

public function products($category_url, $sort){

那么如果您不在其他地方使用它,则不需要任何请求。 另外,您需要删除此行

$sort = $request->get('sort', 'asc');

一切都应该工作。 祝好运! 抱歉,如果无法使用,目前无法测试。

检查此代码中的这三个变量,它将为您提供有关您的问题的更多提示。

public function products($category_url, Request $request){
    Product::getProducts($category_url, self:: $data);
    if ($category1 = Categorie::where('url', '=', $category_url)->first()) {
         dd($category1->id); // 1 check this

         $sort = $request->get('sort', 'asc');
         dd($sort); // 2 check again

         // 3 check 'categorie_id' if there is any typo
         $products = Product::where('categorie_id', $category1->id)
        ->orderBy('price', $sort)
        ->get();

        return view('content.products', self::$data , compact('products', 'sort')); 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM