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
- I need solution to get out brands of products that their id is saved in discount table and list them.
- 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
.
from Laravel Questions and Answers https://laravelquestions.com/php/getting-brands-of-products-with-discounts/
via Lzo Media
No comments:
Post a Comment