I have a table field:
products
-title:New Product
-stock:9
-slug:new-product
And I want is get the additional data 'is_in_stock'. The result will be:
products
-title:New Product
-stock:9
-slug:new-product
-is_in_stock:true
for single data, I can modify the result on the controller itself, but I get stack when getting it to multiple data results. exp. Product::all();
I have read about Eloquent Mutators and Accessors. I have tried the logic on Model files. But I didn't know how to get the result.
This is my Product Model code:
class Product extends Model
{
protected $appends = ['is_in_stock'];
public function getIsInStockAttribute()
{
return $this->attributes['is_in_stock'] = false; // this will be boolean(true/false) result based on the current stock
}
}
Please help for any clue or reference for me to learn.
Thank you in advance :)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire