ldc-dam/app/Models/User.php

177 lines
3.9 KiB
PHP
Raw Normal View History

2019-05-09 15:00:05 +02:00
<?php
2022-04-19 14:49:09 +02:00
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019-05-09 15:00:05 +02:00
use Illuminate\Foundation\Auth\User as Authenticatable;
2022-04-19 10:49:38 +02:00
use Illuminate\Notifications\Notifiable;
2019-05-09 15:00:05 +02:00
class User extends Authenticatable
{
2022-04-19 14:49:09 +02:00
use HasFactory;
2019-05-09 15:00:05 +02:00
use Notifiable;
/**
2022-04-19 14:49:09 +02:00
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
2019-05-09 15:00:05 +02:00
*
2022-04-19 14:49:09 +02:00
* @var array<int, string>
2019-05-09 15:00:05 +02:00
*/
protected $hidden = [
2022-04-19 14:49:09 +02:00
'password',
'remember_token',
2019-05-09 15:00:05 +02:00
];
/**
2022-04-19 14:49:09 +02:00
* The attributes that should be cast.
2019-05-09 15:00:05 +02:00
*
2022-04-19 14:49:09 +02:00
* @var array<string, string>
2019-05-09 15:00:05 +02:00
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
2019-05-16 17:20:30 +02:00
2022-03-28 17:43:52 +02:00
// * 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()
{
2022-07-25 13:09:46 +02:00
return $this->belongsToMany(Marque::class, 'marque_user', 'user_id', 'marque_id');
2022-03-28 17:43:52 +02:00
}
/**
* BelongsToMany dossier relation
* The dossiers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function dossiers()
{
return $this->belongsToMany(Dossier::class);
2022-03-28 17:43:52 +02:00
}
/**
* 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');
2022-03-28 17:43:52 +02:00
}
/**
* HasMany media relation
* Get the media for the user.
2022-03-28 17:43:52 +02:00
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function mediaList()
2022-03-28 17:43:52 +02:00
{
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
*/
2019-07-25 09:43:06 +02:00
public function recherches()
{
2022-03-28 17:43:52 +02:00
return $this->hasMany(Recherche::class);
2019-07-25 09:43:06 +02:00
}
2019-07-31 12:22:24 +02:00
2022-03-28 17:43:52 +02:00
/**
* 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');
2022-03-28 17:43:52 +02:00
}
/**
* BelongsToMany paniers relation
* The paniers that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function paniersFavoris()
{
return $this->belongsToMany(Panier::class);
2022-03-28 17:43:52 +02:00
}
2019-05-09 15:00:05 +02:00
}