2017-07-18 32 views
0

我想訂購產品的價格與一個鏈接從低到高和其他高到低, 但我點擊「由低到高」或「從高到低」的順序沒有改變,它保持在同一頁面上,但url正在改變。發行按照價格在laravel訂購產品

這是在控制器中的功能:

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

在你的控制器試試這個:

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

改變了這個https://pastebin.com/57vFCA3r但仍然順序沒有變化,產品保持相同的順序 –

+0

上@ RO.IT什麼是你的視圖代碼? – ShahinSorkh

+0

這裏https://pastebin.com/KT5KijBi –