61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Scopes\BelongsToPlanner;
|
|
use Database\Factories\UserFactory;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property int $planner_id
|
|
* @property string $name
|
|
* @property Collection<Dish> $dishes
|
|
* @property Collection<UserDish> $userDishes
|
|
* @method static User findOrFail(int $user_id)
|
|
* @method static UserFactory factory($count = null, $state = [])
|
|
* @method static create(array $array)
|
|
* @method static where(string $string, int|string|null $id)
|
|
*/
|
|
class User extends Model
|
|
{
|
|
/** @use HasFactory<UserFactory> */
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'planner_id',
|
|
'name',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::addGlobalScope(new BelongsToPlanner);
|
|
}
|
|
|
|
public function dishes(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Dish::class, 'user_dishes', 'user_id', 'dish_id');
|
|
}
|
|
|
|
public function userDishes(): HasMany
|
|
{
|
|
return $this->hasMany(UserDish::class);
|
|
}
|
|
|
|
public function recurrences(): HasManyThrough
|
|
{
|
|
return $this->hasManyThrough(
|
|
UserDishRecurrence::class,
|
|
UserDish::class,
|
|
'user_id', // Foreign key on user_dishes
|
|
'user_dish_id', // Foreign key on user_dish_recurrences
|
|
'id', // Local key on users
|
|
'id' // Local key on user_dishes
|
|
);
|
|
}
|
|
}
|