Hi everyone, I have a piece of code that I'd like to simplify with a better Eloquent request.
Here's what I have so far:
$localPrices = LocalPrice::whereNotNull('error')
->whereRelation('product', 'monitored', true)
->get();
$errors = [];
foreach($localPrices as $localPrice) {
if($localPrice->product->client->environment == 'prod') {
$errors[] = [
'environment' => $localPrice->product->client->environment,
'client' => $localPrice->product->client->client_display_name,
'country' => $localPrice->country->name,
'product' => $localPrice->product->name,
'error' => $localPrice->error,
];
}
}
The LocalPrice model has two foreign keys: product_id, country_id.
The Product model has one foreign key: country_id.
I would like to update my request to only get LocalPrice that are related to a **product **that are related to a **client **whose environment is 'prod'.
I'd like to avoid the if() part but I don't know how to get this "second degree" relationship.
Could you please point me to the right direction? Thanks!