简体   繁体   中英

getting brands of products with discounts

I try to make search in brands where thy have discounted products.

Discount Logic

Discount table will save product_id , and product has brand_id

What I want

  1. I need solution to get out brands of products that their id is saved in discount table and list them.
  2. After that I want to select each of those brands and show result of products which has discount and has this brand id.

Codes

here is how I get my discounted products:

$promotions = Discount::with('products')->Valid()->orderby('id', 'desc')->paginate(12);

This is my brands:

$brandspromotion = Brand::OfStatus('Active')->get();

here is my full code:

        // Index `Promotion` page (where all results are mixed from all brands etc.)
            public function promotions() {
                $promotions = Discount::with('products')->Valid()->orderby('id', 'desc')->paginate(12); // get all discounted products
                $options = Option::all(); // for my sidebar only
                $brands = Brand::OfStatus('Active')->get(); // for my sidebar only



                $brandspromotion = Brand::OfStatus('Active')->get();
//this has to be change to some query in order to only get brands of discounted products
//result of this i will use in drop-down select box.




                return view('front.promotions', compact('promotions', 'options', 'brandspromotion', 'brands'));
              }

    // this function will take care of chosen brand result
          public function brandspromotions($slug) {
            //
          }

any idea?

UPDATE

I changed my query to something like code below:

$promotions2 = Discount::with('products')->Valid()->orderby('id', 'desc')->get();
    foreach($promotions2 as $brandpro){
      $brand_id = $brandpro->products->brand_id;
    }
    $brandspromotion = Brand::OfStatus('Active')
    ->where('id', '=', $brand_id)->get();

It is working but only gets 1 brand, something with my loop has to be wrong!

Update 2

I tried to use map here is my code:

$promotions2 = Discount::with('products')->Valid()->get();
    $grouped = $promotions2->map(function ($item, $key) {
      return $item->products->brand_id;
    });
    $grouped->all();
    // dd($grouped);
    $brandspromotion = Brand::OfStatus('Active')
    ->where('id', '=', $grouped)->get();

result is like:

Collection {#713 ▼
  #items: array:4 [▼
    0 => 2
    1 => 1
    2 => 2
    3 => 1
  ]
}

I suppose to get lenovo (id:1) and lg (id:2) but i only get lg .

UPDATE 3

$promotions2 = Discount::with('products')->Valid()->get();
$grouped = $promotions2->mapToGroups(function ($item, $key) {
    return [$item->products->brand_id => $item->products->brand_id];
});
dd($grouped);
$brandspromotion = Brand::OfStatus('Active')
->where('id', '=', $grouped->all())->get();

Result:

Collection {#704 ▼
  #items: array:2 [▼
    2 => Collection {#710 ▼
      #items: array:2 [▼
        0 => 2
        1 => 2
      ]
    }
    1 => Collection {#703 ▼
      #items: array:2 [▼
        0 => 1
        1 => 1
      ]
    }
  ]
}

Issue:

I suppose to get lenovo (id:1) and lg (id:2) but i only get lg .

您可以使用whereIn来传递值数组:

$brandspromotion = Brand::OfStatus('Active')->whereIn('id', $grouped)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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