Door.php 1.13 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1 2 3 4
<?php

namespace App;

5
use Illuminate\Database\Eloquent\Relations\HasMany;
Jacob Priddy's avatar
Jacob Priddy committed
6
use Illuminate\Foundation\Auth\User as Authenticatable;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
Jacob Priddy's avatar
Jacob Priddy committed
8

Jacob Priddy's avatar
Jacob Priddy committed
9 10
class Door extends Authenticatable
{
11 12 13
    protected $fillable = ['id', 'name', 'location', 'created_at', 'updated_at', 'last_seen_at'];

    protected $dates = ['last_seen_at'];
Jacob Priddy's avatar
Jacob Priddy committed
14

15 16 17 18 19 20 21 22
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class);
    }

23 24 25 26 27 28 29 30
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function overrides(): HasMany
    {
        return $this->hasMany(Override::class);
    }

31
    public static function boot(): void
32 33 34 35 36 37
    {
        parent::boot();

        static::deleting(static function (Door $door) {
            // Detach all groups
            $door->groups()->detach();
38

39 40 41 42 43
            // Delete all overrides
            /** @var \App\Override $override */
            foreach ($door->overrides() as $override) {
                $override->delete();
            }
44 45
        });
    }
Jacob Priddy's avatar
Jacob Priddy committed
46
}