Not tested but this should return a collection of alerts with product/user eager loaded grouped by user_id whose alert price is less than or equal to the product price.
$alerts = $alerts->with('products', 'users')->all(); $filtered = $alerts->filter(function($alert) { return ($alert->price <= $alert->product->salesprice) })->groupBy('user_id');
$alerts = Alert::with(['product','user'])->where('amount', '<=', $salePrice)->get();
Though I would move this to a repository or a scope query in the least if you find that you're repeating yourself.
orecrush - i think he wants all the alerts which have a price lower than than the sales price, not which alerts are less than a specified price.
Cool, Thanks. I'll give this a shot. Yeah, sale price, less than alert amount ( I had this wrong in my query, but not really a core issue of this post).
@KevinCreel - Gotcha thanks.
$alerts = Alert::with(['product','user'])->whereHas('product', function($q){
$q->where('saleprice1', '<', 'alert.amount');
})->get();
Didn't test it but something like that would also work. Good luck!
PS - I'd definitely put this in a scope query though. So you could do something like
Alert::lowerPrices()->get();
Nice, this did the trick. Thanks for the help, already starting to see how to optimize other queries using eloquent relationshipos instead of DB::raw.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community