User.php 1.55 KB
Newer Older
1
2
3
4
<?php

namespace App;

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

Jacob Priddy's avatar
Jacob Priddy committed
10
11
class User extends Authenticatable
{
Jacob Priddy's avatar
Jacob Priddy committed
12
    use SoftDeletes;
13

Jacob Priddy's avatar
Jacob Priddy committed
14
15
16
17
    protected $fillable = [
        '*',
    ];

18
19
20
21
22
23
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
Jacob Priddy's avatar
Jacob Priddy committed
24
        'expires_at' => 'datetime',
25
    ];
Jacob Priddy's avatar
Jacob Priddy committed
26
27

    /**
28
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
Jacob Priddy's avatar
Jacob Priddy committed
29
     */
Jacob Priddy's avatar
Jacob Priddy committed
30
31
    public function tokens(): HasMany
    {
Jacob Priddy's avatar
Jacob Priddy committed
32
33
        return $this->hasMany(Token::class);
    }
34

35
36
37
38
39
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function entries(): HasMany
    {
40
        return $this->hasMany(Entry::class);
41
42
    }

43
44
45
46
47
48
49
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class);
    }
50
51
52
53
54
55
56
57
58
59
60
61
62
63

    public static function boot()
    {
        parent::boot();

        static::deleting(static function (User $user) {
            // Detach all groups
            $user->groups()->detach();

            // Delete all tokens
            /** @var \App\Token $token */
            foreach ($user->tokens() as $token) {
                $token->delete();
            }
64
65
66
67
68

            /** @var \App\Entry $entry */
            foreach ($user->entries() as $entry) {
                $entry->delete();
            }
69
70
        });
    }
71
}