/ app / Models / Entity.php
Entity.php
  1  <?php
  2  
  3  namespace App\Models;
  4  
  5  use App\Enums\EntityTypeEnum;
  6  use App\Enums\RoleEnum;
  7  use Illuminate\Database\Eloquent\Factories\HasFactory;
  8  use Illuminate\Database\Eloquent\Model;
  9  use Illuminate\Database\Eloquent\Relations\MorphMany;
 10  use Illuminate\Database\Eloquent\Relations\MorphOne;
 11  use Illuminate\Database\Eloquent\Relations\MorphToMany;
 12  use Illuminate\Database\Eloquent\SoftDeletes;
 13  use Illuminate\Support\Collection;
 14  
 15  class Entity extends Model
 16  {
 17      protected $guarded = [];
 18  
 19      use HasFactory, SoftDeletes;
 20  
 21      /**
 22       * The users that belong to the role.p
 23       */
 24      public function users(): MorphToMany
 25      {
 26          return $this->morphToMany(User::class, 'entity', 'entity_user');
 27      }
 28  
 29      public function representatives()
 30      {
 31          return $this->users()
 32              ->wherePivot('entity_id', $this->id)
 33              ->wherePivot('entity_type', self::class)
 34              ->wherePivot('role_id', RoleEnum::representative()->value)
 35              ->get();
 36      }
 37  
 38      public function toes()
 39      {
 40          return $this->hasMany(TOE::class);
 41      }
 42  
 43      public function country()
 44      {
 45          return $this->belongsTo(Country::class, 'country_id');
 46      }
 47  
 48      public function keys(): MorphMany
 49      {
 50          return $this->morphMany(GpgKey::class, 'entity');
 51      }
 52  
 53      public function activeKey(): MorphOne
 54      {
 55          return $this->morphOne(GpgKey::class, 'entity');
 56      }
 57  
 58      public function type()
 59      {
 60          return $this->belongsTo(EntityType::class, 'entity_type_id');
 61      }
 62  
 63      public function audits()
 64      {
 65          return $this->hasMany(Audit::class);
 66      }
 67  
 68      public function dossiers()
 69      {
 70          return $this->hasMany(Dossier::class, 'laboratory_id');
 71      }
 72  
 73      public function contactInfo(): MorphOne
 74      {
 75          return $this->morphOne(ContactInfo::class, 'contactable');
 76      }
 77  
 78      public function outputs(): MorphToMany
 79      {
 80          return $this->morphToMany(OutPut::class, 'outputable');
 81      }
 82  
 83      public function pointsOfContact(): Collection
 84      {
 85          return $this->users()->withPivot('is_poc')->where('is_poc', true)->get();
 86      }
 87  
 88      //Attributes
 89      public static function cab(): Entity
 90      {
 91          return Entity::where('entity_type_id', EntityTypeEnum::cab())->first();
 92      }
 93      /**
 94       * PHPWord
 95       * @return Attribute
 96       */
 97      public function getEmailsAttribute()
 98      {
 99          return implode(',', $this->pointsOfContact()->pluck('email')->toArray());
100      }
101  
102      /**
103       * PHPWord
104       * @return Attribute
105       */
106      public function getReasonAttribute()
107      {
108          return $this->legal_name;
109      }
110  }