mardi 18 juillet 2017

Isuue in query(ordering products)

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. I tried to debug the query and I got: "select * from products where categorie_id = ? order by price asc" is that means that it can't find the id from categories table? if it is, I can't find whats the problem

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire