I have a relation method for a model which has a condition based on a property of the model itself.
// ProductController.php
public function show($id, Request $request) {
$product = Product::find($id);
if ($request->exists('optionValues') {
$product->load('optionValues');
}
}
// Product.php
public function optionValues()
{
/// here $this->stock_status_id is null. actually all attributes array is empty.
if ($this->stock_status_id == Stock::CUSTOM_ORDER) {
return $this->hasMany(ProductOptionValue::class, 'product_id', 'product_id')
->where('status', 1);
}
return $this->hasMany(ProductOptionValue::class, 'product_id', 'product_id')
->where('price', '>', 0)
->where('quantity', '>', '0')
->where('status', 1);
}
but it seems when Laravel is loading a relation all properties are null.
and $this->stock_status_id for current model is null, and I can't check for the condition.
Is there any workaround this problem?
$product = Product::find($request->id), I call$product->load('optionValues')request('optionValues')) parameter.