LMIV-SNACKAUTOMAT/app/Models/Slot.php

45 lines
1022 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Slot extends Model
{
protected $fillable = [
'vending_machine_id',
'slot_number',
'position',
'capacity',
'is_active'
];
protected $casts = [
'is_active' => 'boolean',
];
public function vendingMachine(): BelongsTo
{
return $this->belongsTo(VendingMachine::class);
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'slot_product')
->withPivot('quantity', 'current_price')
->withTimestamps();
}
public function currentProduct()
{
return $this->products()->wherePivot('quantity', '>', 0)->first();
}
public function getTotalQuantityAttribute()
{
return $this->products()->sum('slot_product.quantity');
}
}