Add it to the $appends array on the model to automatically include it in the array and JSON representations of the Model:
protected $appends = [
'stock_left'
];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everybody, yesterday I discovered accesors thanks to the community:
https://laracasts.com/discuss/channels/general-discussion/attach-property-to-eloquent-model
They appear to be really handy computed properties for my backend models HOWEVER, I just noticed that they are not retained in the response. That is, on my vue components I don't have access to these properties, they aren't just there. This is SUCH a bummer...
So my question is, how can I make the accessors be retained in the JSON response so I can use these in my views!
So I can use discount.stockLeft in my vue components.
My accessor example:
public function getStockLeftAttribute()
{
return collect($this->discountcodes)->reject(function ($code) {
return $code->isBought;
})->count();
}
Add it to the $appends array on the model to automatically include it in the array and JSON representations of the Model:
protected $appends = [
'stock_left'
];
Please or to participate in this conversation.