Door.php 1.83 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 31 32 33 34 35 36 37 38
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function entries(): HasMany
    {
        return $this->hasMany(Entry::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function attempts(): HasMany
    {
        return $this->hasMany(Attempt::class);
    }

39 40 41 42 43 44 45 46
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function overrides(): HasMany
    {
        return $this->hasMany(Override::class);
    }

47
    public static function boot(): void
48 49 50 51 52 53
    {
        parent::boot();

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

55 56 57 58 59 60 61 62 63 64 65 66
            // Delete all entries
            /** @var \App\Entry $entry */
            foreach ($door->entries() as $entry) {
                $entry->delete();
            }

            // Delete all attempts
            /** @var \App\Attempt $attempt */
            foreach ($door->attempts() as $attempt) {
                $attempt->delete();
            }

67 68 69 70 71
            // Delete all overrides
            /** @var \App\Override $override */
            foreach ($door->overrides() as $override) {
                $override->delete();
            }
72 73
        });
    }
Jacob Priddy's avatar
Jacob Priddy committed
74
}