jeremykenedy /
laravel-auth
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Models; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
| 7 | use Illuminate\Foundation\Auth\User as Authenticatable; |
||
| 8 | use Illuminate\Notifications\Notifiable; |
||
| 9 | use jeremykenedy\LaravelRoles\Traits\HasRoleAndPermission; |
||
| 10 | |||
| 11 | class User extends Authenticatable |
||
| 12 | { |
||
| 13 | use HasRoleAndPermission; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 14 | use Notifiable; |
||
|
0 ignored issues
–
show
|
|||
| 15 | use SoftDeletes; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The database table used by the model. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $table = 'users'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Indicates if the model should be timestamped. |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | public $timestamps = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The attributes that are not mass assignable. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $guarded = [ |
||
| 37 | 'id', |
||
| 38 | ]; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The attributes that are hidden. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $hidden = [ |
||
| 46 | 'password', |
||
| 47 | 'remember_token', |
||
| 48 | 'activated', |
||
| 49 | 'token', |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The attributes that should be mutated to dates. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $dates = [ |
||
| 58 | 'created_at', |
||
| 59 | 'updated_at', |
||
| 60 | 'deleted_at', |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The attributes that are mass assignable. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $fillable = [ |
||
| 69 | 'name', |
||
| 70 | 'first_name', |
||
| 71 | 'last_name', |
||
| 72 | 'email', |
||
| 73 | 'password', |
||
| 74 | 'activated', |
||
| 75 | 'token', |
||
| 76 | 'signup_ip_address', |
||
| 77 | 'signup_confirmation_ip_address', |
||
| 78 | 'signup_sm_ip_address', |
||
| 79 | 'admin_ip_address', |
||
| 80 | 'updated_ip_address', |
||
| 81 | 'deleted_ip_address', |
||
| 82 | ]; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The attributes that should be cast to native types. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $casts = [ |
||
| 90 | 'id' => 'integer', |
||
| 91 | 'first_name' => 'string', |
||
| 92 | 'last_name' => 'string', |
||
| 93 | 'email' => 'string', |
||
| 94 | 'password' => 'string', |
||
| 95 | 'activated' => 'boolean', |
||
| 96 | 'token' => 'string', |
||
| 97 | 'signup_ip_address' => 'string', |
||
| 98 | 'signup_confirmation_ip_address' => 'string', |
||
| 99 | 'signup_sm_ip_address' => 'string', |
||
| 100 | 'admin_ip_address' => 'string', |
||
| 101 | 'updated_ip_address' => 'string', |
||
| 102 | 'deleted_ip_address' => 'string', |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the socials for the user. |
||
| 107 | */ |
||
| 108 | public function social() |
||
| 109 | { |
||
| 110 | return $this->hasMany(\App\Models\Social::class); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get the profile associated with the user. |
||
| 115 | */ |
||
| 116 | public function profile() |
||
| 117 | { |
||
| 118 | return $this->hasOne(\App\Models\Profile::class); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The profiles that belong to the user. |
||
| 123 | */ |
||
| 124 | public function profiles() |
||
| 125 | { |
||
| 126 | return $this->belongsToMany(\App\Models\Profile::class)->withTimestamps(); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Check if a user has a profile. |
||
| 131 | * |
||
| 132 | * @param string $name |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function hasProfile($name) |
||
| 137 | { |
||
| 138 | foreach ($this->profiles as $profile) { |
||
| 139 | if ($profile->name === $name) { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return false; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Add/Attach a profile to a user. |
||
| 149 | * |
||
| 150 | * @param Profile $profile |
||
| 151 | */ |
||
| 152 | public function assignProfile(Profile $profile) |
||
| 153 | { |
||
| 154 | return $this->profiles()->attach($profile); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Remove/Detach a profile to a user. |
||
| 159 | * |
||
| 160 | * @param Profile $profile |
||
| 161 | */ |
||
| 162 | public function removeProfile(Profile $profile) |
||
| 163 | { |
||
| 164 | return $this->profiles()->detach($profile); |
||
| 165 | } |
||
| 166 | } |
||
| 167 |