mardi 18 juillet 2017

Issue to order products by price in laravel

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

  <a href="  " style="color:black"> High to low</a> |
  <a href="  " 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();
        }
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2vcXNG6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire