Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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');

Last updated 2 years ago.
0
$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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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).

Last updated 2 years ago.
0

@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();
Last updated 2 years ago.
0

@onecrush,

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mrgauthier mrgauthier Joined 11 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.