ldc-dam/app/Models/User.php
2022-07-25 13:09:46 +02:00

176 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// * METHODS
/**
* Authorize role
*
* @param string|array $roles
* @return boolean
*/
public function authorizeRoles($roles): bool
{
if (is_array($roles) and in_array($this->role, $roles)) {
return true;
}
if (is_string($roles) and $this->role == $roles) {
return true;
}
abort(401, 'Cette action n\'est pas autorisée.');
}
/**
* Check one role
*
* @param array|string $roles
* @return boolean
*/
public function hasRole($roles): bool
{
if (is_array($roles)) {
return in_array($this->role, $roles);
}
return $this->role == $roles;
}
/**
* Get role label
*
* @return string|null
*/
public function getRoleLabel(): ?string
{
return config('roles.label.' . $this->role);
}
// * RELATIONSHIPS
/**
* BelongsToMany marque relation
* The marques that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function marques()
{
return $this->belongsToMany(Marque::class, 'marque_user', 'user_id', 'marque_id');
}
/**
* BelongsToMany dossier relation
* The dossiers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function dossiers()
{
return $this->belongsToMany(Dossier::class);
}
/**
* BelongsToMany sujet relation
* The sujet that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sujets()
{
return $this->belongsToMany(Sujet::class, 'sujet_user', 'user_id', 'sujet_id');
}
/**
* HasMany media relation
* Get the media for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function mediaList()
{
return $this->hasMany(Media::class);
}
/**
* HasMany historiques relation
* Get the historiques for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function historiques()
{
return $this->hasMany(Historique::class);
}
/**
* HasMany recherches relation
* Get the recherches for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function recherches()
{
return $this->hasMany(Recherche::class);
}
/**
* HasMany paniers relation
* Get the paniers for the user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function paniersOwner()
{
return $this->hasMany(Panier::class, 'user_id')->with('mediaList');
}
/**
* BelongsToMany paniers relation
* The paniers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function paniersFavoris()
{
return $this->belongsToMany(Panier::class);
}
}