37 lines
903 B
PHP
37 lines
903 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class EggCollection extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
public function items(): HasMany
|
|
{
|
|
return $this->hasMany(EggCollectionItem::class);
|
|
}
|
|
|
|
public function syncTotals()
|
|
{
|
|
$this->total_eggs = $this->items()->where('is_broken', false)->sum('quantity');
|
|
$this->total_broken_eggs = $this->items()->where('is_broken', true)->sum('quantity');
|
|
$this->saveQuietly();
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'production_date' => 'datetime',
|
|
'total_eggs' => 'integer',
|
|
'total_broken_eggs' => 'integer',
|
|
];
|
|
}
|
|
}
|