按价格订购产品的问题

时间:2017-07-18 04:16:53

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

我想按价格订购产品,一个链接从低到高,另一个从高到低, 但是,在我点击“从低到高”或“从高到低”后,订单不会改变,它会停留在同一页面上,但网址会发生变化。

这是控制器中的功能:

    myAppService.getPDFPathFileName= function(){
    //logic
    var Url = myURL+'/getFiles/getPDF.form';
     ̲$http.get(repUrl).then(
    function (response) {
       deferred.resolve(response.data);}
    ̶function(errResponse){
        deferred.reject(errResponse);
        });
return deferred.promise;
    }

这是路线:

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

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

  Route::get('shop/{category_url}/sorting-{sort?}', 'ShopController@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>

3 个答案:

答案 0 :(得分:0)

我觉得你的情况存在问题,但对于这个问题,页面没有改变。我想你可以在if声明之外回复。所以代码应该是这样的:

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

它会给你一些小错误,但我认为你的页面会改变:D

答案 1 :(得分:0)

在视图中 -

<a href="{{ url('shop/'.$category['url'].'/sorting')}}?sort=ASC" style="color:black"> High to low</a>

在路线 -

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

在控制器中 -

 public function products(Request $request, $category_url)
{
    $data = Input::all();

    Product::getProducts($category_url, self:: $data);

    if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) 
        {

            if( !isset($data['sort']) )
               $data['sort'] = 'ASC';

    $products = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $data['sort'])->get();

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

答案 2 :(得分:0)

我认为您应该在数据中设置产品。

if ( $category1 = Categorie::where('url', '=', $category_url)->first() ) {
    self::$data['products'] = Product::where('categorie_id', $category1->getAttribute('id'))->orderBy('price', $sort)->get();
}