php - Laravel eager loading relation with condition on model properties
Get the solution ↓↓↓I have a relation that has awhere
condition based on one of the properties of the main model.
// Product model
public function optionValues() {
$relation = $this->hasMany(ProductOptionValue::class);
if ($this->stock_status_id == Product::CustomStock) {
return $relation->where('status', 1);
}
return $relation
->where('price', '>', 0)
->where('quantity', '>', 0)
->where('status', 1);
}
I try to find a product with eager loaded relations like this:
Product::with([..., 'optionValues'])->find($productId);
but when the code reaches the relation function,$this
has no properties.
so always$this->stock_status_id
is null and condition does not work.
Is there a workaround for this issue?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the process class relies on proc_open, which is not available on your php installation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.