LaboratoryRepresentative.php
1 <?php 2 3 namespace App\Models; 4 5 use Illuminate\Database\Eloquent\Factories\HasFactory; 6 use Illuminate\Database\Eloquent\Model; 7 use Spatie\Activitylog\LogOptions; 8 use Spatie\Activitylog\Traits\LogsActivity; 9 10 class LaboratoryRepresentative extends Model 11 { 12 use HasFactory, LogsActivity; 13 14 protected $fillable = [ 15 'name', 16 'dni', 17 'username', 18 'position', 19 'email', 20 'password', 21 'gpg', 22 'gpg_pass', 23 'laboratory_id', 24 ]; 25 26 protected $hidden = [ 27 'password', 28 'gpg', 29 'gpg_pass', 30 ]; 31 32 public function laboratory() 33 { 34 return $this->belongsTo(Laboratory::class); 35 } 36 37 public function laboratoryLogs() 38 { 39 return $this->hasMany(LaboratoryLog::class); 40 } 41 42 public function meetAssistants() 43 { 44 return $this->hasMany(MeetAssistant::class); 45 } 46 47 public function meets() 48 { 49 $assistants = $this->meetAssistants; 50 51 $meetIds = []; 52 53 foreach ($assistants as $assistant) { 54 $meetIds[$assistant->meet_id] = true; 55 } 56 57 return Meet::whereIn('id', array_keys($meetIds))->get(); 58 } 59 60 public function isInMeet(Meet $meet) 61 { 62 $labReps = $meet->assistants->where('meet_assistant_type_id', MeetAssistantType::LAB_REP)->toArray(); 63 64 return in_array($this->id, array_column($labReps, 'id')); 65 } 66 67 public function getActivitylogOptions(): LogOptions 68 { 69 return LogOptions::defaults() 70 ->logOnly([ 71 'name', 72 'dni', 73 'username', 74 'position', 75 'email', 76 'laboratory_id', 77 'laboratory.name', 78 ])->logOnlyDirty() 79 ->dontSubmitEmptyLogs(); 80 } 81 }