Hi everybody, I was wondering if there's a way to attach an atribute to eloquent collections , like I want to calculate the ritems available from looping through items array.
I know you could make this by looping through each item and increasing the country but how can I automate the proccess for each model instance, so I can make a select input range with $stockLeft.
Here is my controller method:
public function descuentoSlug( $slug )
{
$discount = Discount::getBySlug($slug)->toArray();
$stockLeft = 0;
foreach($discount['discountcodes'] as $code){
if($code['isBought'] == false){
$stockLeft++;
}
}
$discount['stockLeft'] = $stockLeft;
$meta =
[
'title' => $discount['title'],
'description' => '',
'breadcumbs' => [ 'Descuentos', $discount['title'] ]
];
return view('descuentos.show', compact('meta', 'discount'));
}
How van I attach a computed atribute to Discount Eloquent Model?