2017-07-18 88 views
-1

我想按价格订购产品,一个链接从低到高,另一个从高到低,但点击“从低到高”或“从高到低”顺序不是改变,它保持在同一页面上,但网址正在改变。 我试图调试查询,我得到:“select * from products where categorie_id =?order by price asc”是否意味着它不能从类别表中找到id? 如果是,我找不到什么问题Isuue在查询中(订购产品)

这是控制器的功能:

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')); 
    } 
} 

这是路线:

Route::get('shop/{category_url}/sorting-{sort?}', '[email protected]'); 

这些都是从链接查看,视图为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> 

该型号:

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(); 
     } 
    } 
} 

回答

0

这里是我会怎么做:

public function products(Request $request, $category_url, $sort= 'ASC'){ 

    $category1 = Categorie::where('url', '=', $category_url)->first(); 
    $products = Product::where('categorie_id', $category1->id)->orderBy('price', $sort)->get(); 
    return view('content.products', ['products' => $products, 'sort' => $sort, 'category' => $category1]); 
} 
+0

这样我得到了一个未定义的变量:类(查看:C:\ xampp5 \ htdocs中\ myshop \资源\意见\内容\产品。 blade.php) –

+0

所以我试图添加这个:https://pastebin.com/BZrfuwRK但它仍然没有改变订单 –

+0

我刚刚编辑previus的答案。增加了参数类别。 – Tim