'bool' ]; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'libelle', 'description', 'is_private', 'parent_id' ]; // * GETTERS /** * Get user favoris * * @return boolean */ public function getFavorisAttribute(): bool { $userId = Auth::user()->id; if ($this->users()->where('id', $userId)->first()) { return true; } else { return false; } } /** * Get media linked * * @return integer */ public function getMediacountAttribute(): int { /** @var \App\Model\User $user */ $user = Auth::user(); // ? Est un dossier dépot ou racine ? if ($this->parent_id === 1 or $this->id === 1) { // * Dossier dépôt return $this->mediaList()->count(); } // * Dossier classic // ? User is admin ? if ($user->hasRole(config('roles.const.ADMIN'))) { // * Tout de tout le monde return $this->mediaList()->count(); } // * Tous les non brouillons ou crées par le user return $this->mediaList() ->where(fn (Builder $q) => $q->where('statut', '!=', MediaStatut::brouillon) ->orWhere('created_by', $user->id)) ->count(); } // * RELATIONSHIPS /** * BelongsToMany marque relation * The marques that belong to the dossier. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function marques() { return $this->belongsToMany(Marque::class, 'marque_dossier', 'dossier_id', 'marque_id'); } /** * BelongsTo sujet relation * Get the sujet that owns the dossier. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function sujet() { return $this->belongsTo(Sujet::class, 'sujet_id'); } /** * HasMany media relation * Get the dossier for the media. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function mediaList() { return $this->hasMany(Media::class); } /** * HasMany dossier relation * Get the dossiers for the parent. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function sousdossiers() { return $this->hasMany(Dossier::class, 'parent_id'); } /** * BelongsTo dossier relation * Get the parent that owns the dossiers. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function dossierParent() { return $this->belongsTo(Dossier::class, 'id', 'parent_id'); } /** * BelongsToMany user relation * The users that belong to the dossier. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function users() { return $this->belongsToMany(User::class); } // * METHODS /** * Find Dossier by subject id * * @param integer $sujetId * @return Dossier|null */ public static function getDossierBySujetid(int $sujetId) { return Dossier::where('sujet_id', $sujetId)->whereNull('parent_id')->get(); } }