vendredi 15 juillet 2022

laravel products with the highest sales will be displayed at the top

I am trying to sort products with the highest sales will be displayed at the top. Do I need to improve it? or is it correct enough to get sorted products with highest sales?

Here is my sort query

    $data = Product::query();
    if ($category_id) {
        $data->where('category_id', $category_id);
    }
    if ($theme_id) {
        $data->Where('theme_id', 'LIKE', '%' . $theme_id . '%');
    }
    if ($sub_category_id) {
        $data->where('sub_category_id', $sub_category_id);
    }

    $data = $data->leftJoin('order_items', function ($join) {
              $join->on('products.id', '=', 'order_items.product_id');
    })
    ->selectRaw('products.*, sum(order_items.id) as total')
    ->groupBy('products.id')
    ->orderByDesc('total');

    $products = excludeProductLessThanZero($data->paginate($this->paginationcount));

products table

enter image description here

**Orders table**

enter image description here

**order_items**

enter image description here

All three tables database structure is given above. Please guide and suggested. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire