简体   繁体   English

发行按价格订购产品

[英]Issue to order products by price

I want to order products by price with one link for low to high and the other high to low, But after I click "low to high" or "high to low" the order is not changing, it stays on the same page but the url is changing. 我想按价格从低到高和另一个从高到低的链接订购产品,但是当我单击“从低到高”或“从高到低”时,订单没有改变,它停留在同一页面上,但是网址正在更改。

this is the function in the controller: 这是控制器中的功能:

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

this is the route: 这是路线:

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

those are the link from the view, the view is content.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>

the model: 该模型:

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

I think there's problem in your condition, but for the problem the page is not change. 我认为您的情况存在问题,但是对于该问题,页面没有更改。 I think you can make your return outside the if statement. 我认为您可以在if语句之外返回。 So the code should be like this: 因此,代码应如下所示:

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

It will give you little error, but I think your page will change :D 它不会给您带来什么错误,但是我认为您的页面将会改变:D

In View - 在视野中-

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

In Route - 途中-

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

In Controller - 在控制器中-

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

I think you should set products in data. 我认为您应该在数据中设置产品。

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

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

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