dishplanner/app/Models/Schedule.php

66 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2025-10-13 14:57:11 +02:00
<?php
namespace App\Models;
use App\Models\Scopes\BelongsToPlanner;
use Carbon\Carbon;
use Closure;
use Database\Factories\ScheduleFactory;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Query\Expression;
/**
* @property int $id
* @property int $planner_id
* @property int $dish_id
* @property int $user_id
* @property Planner $planner
* @property Dish $dish
* @property User $user
* @property Carbon $date
2026-08-17 13:30:49 +02:00
* @property bool $is_skipped
2025-10-13 14:57:11 +02:00
* @property Collection<ScheduledUserDish> $scheduledUserDishes
2026-08-17 13:30:49 +02:00
*
2025-10-13 14:57:11 +02:00
* @method static create(array $array)
* @method static Builder where(array|Closure|Expression|string $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
* @method static ScheduleFactory factory($count = null, $state = [])
* @method static firstOrCreate(array $array, false[] $array1)
2025-10-13 14:57:11 +02:00
*/
class Schedule extends Model
{
/** @use HasFactory<ScheduleFactory> */
use HasFactory;
protected $table = 'schedules';
public $timestamps = false;
2026-08-17 13:30:49 +02:00
protected $dateFormat = 'Y-m-d';
2025-10-13 14:57:11 +02:00
protected $fillable = ['planner_id', 'date', 'is_skipped'];
protected $casts = [
'date' => 'date',
'is_skipped' => 'boolean',
];
protected static function booted(): void
{
static::addGlobalScope(new BelongsToPlanner);
}
public function scheduledUserDishes(): HasMany
{
return $this->hasMany(ScheduledUserDish::class);
}
public function hasAllUsersScheduled(): bool
{
return $this->scheduledUserDishes->count() === User::where('planner_id', $this->planner_id)->count();
2025-10-13 14:57:11 +02:00
}
}