查询中的问题(订购产品)

时间:2017-07-18 09:37:19

标签: php laravel laravel-5 laravel-5.2 laravel-5.1

我想按价格订购产品,一个链接从低到高,另一个从高到低,但点击"从低到高"或者"从高到低"订单没有变化,它保持在同一页面,但网址正在变化。 我尝试调试查询,然后得到:"从products中选择*,其中categorie_id =?按price asc"排序这意味着它无法从类别表中找到id? 如果是的话,我无法找到问题

这是控制器中的功能:

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?}', '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>

模特:

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

1 个答案:

答案 0 :(得分: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]); 
}