ldc-dam/app/Dossier.php
2019-05-22 17:10:16 +02:00

87 lines
1.6 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Dossier extends Model
{
protected $with = "sousdossiers";
protected $appends = array('favoris', 'mediacount');
/**
* Get user favoris
*/
public function getFavorisAttribute()
{
$userId = Auth::user()->id;
if($this->users()->where('id', $userId)->first()){
return true;
}else{
return false;
}
}
/**
* Get media linked
*/
public function getMediacountAttribute()
{
return $this->medias()->count();
}
/**
* The marques that belong to the dossier.
*/
public function marques()
{
return $this->belongsToMany('App\Marque', 'marques_dossiers', 'dossier_id', 'marque_id');
}
/**
* Get the sujet that owns the dossier.
*/
public function sujet()
{
return $this->belongsTo('App\Sujet', 'sujet_id');
}
/**
* Get the dossier for the sujet.
*/
public function medias()
{
return $this->hasMany('App\Media', 'dossier_id', 'id');
}
/**
* Get the dossiers for the parent.
*/
public function sousdossiers()
{
return $this->hasMany('App\Dossier', 'parent_id');
}
/**
* Get the parent that owns the dossiers.
*/
public function dossierParent()
{
return $this->belongsTo('App\Dossier', 'id', 'parent_id');
}
/**
* The users that belong to the dossier.
*/
public function users()
{
return $this->belongsToMany('App\User', 'dossiers_users', 'dossier_id', 'user_id');
}
public static function getDossierBySujetid($sujetId)
{
return Dossier::where('sujet_id', $sujetId)->whereNull('parent_id')->get();
}
}