Created
September 18, 2021 04:49
-
-
Save AnthoniG/a3369a396ccf3ed60ff3c14b7beca885 to your computer and use it in GitHub Desktop.
Example of IsAdmin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Carbon\Carbon; | |
use Hash; | |
use Illuminate\Auth\Notifications\ResetPassword; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Laravel\Passport\HasApiTokens; | |
use \DateTimeInterface; | |
class User extends Authenticatable | |
{ | |
use SoftDeletes, Notifiable, HasApiTokens; | |
public $table = 'users'; | |
protected $hidden = [ | |
'remember_token', | |
'password', | |
]; | |
protected $dates = [ | |
'email_verified_at', | |
'created_at', | |
'updated_at', | |
'deleted_at', | |
]; | |
protected $fillable = [ | |
'name', | |
'email', | |
'email_verified_at', | |
'password', | |
'remember_token', | |
'created_at', | |
'updated_at', | |
'deleted_at', | |
]; | |
protected function serializeDate(DateTimeInterface $date) | |
{ | |
return $date->format('Y-m-d H:i:s'); | |
} | |
public function getIsAdminAttribute() | |
{ | |
return $this->roles()->where('id', 1)->exists(); | |
} | |
public function getEmailVerifiedAtAttribute($value) | |
{ | |
return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('panel.date_format') . ' ' . config('panel.time_format')) : null; | |
} | |
public function setEmailVerifiedAtAttribute($value) | |
{ | |
$this->attributes['email_verified_at'] = $value ? Carbon::createFromFormat(config('panel.date_format') . ' ' . config('panel.time_format'), $value)->format('Y-m-d H:i:s') : null; | |
} | |
public function setPasswordAttribute($input) | |
{ | |
if ($input) { | |
$this->attributes['password'] = app('hash')->needsRehash($input) ? Hash::make($input) : $input; | |
} | |
} | |
public function sendPasswordResetNotification($token) | |
{ | |
$this->notify(new ResetPassword($token)); | |
} | |
public function roles() | |
{ | |
return $this->belongsToMany(Role::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment