/ app / Models / OutPut.php
OutPut.php
 1  <?php
 2  
 3  namespace App\Models;
 4  
 5  use Illuminate\Database\Eloquent\Factories\HasFactory;
 6  use Illuminate\Database\Eloquent\Model;
 7  use Illuminate\Database\Eloquent\Relations\MorphTo;
 8  use Illuminate\Database\Eloquent\Relations\MorphToMany;
 9  
10  class OutPut extends Model
11  {
12      use HasFactory;
13  
14      protected $fillable = ['description', 'communication_type_id', 'communication_date',  'type_id', 'task_id', 'head_id', 'dossier_id', 'created_by', 'send_to_entitable_id', 'send_to_entitable_type', 'is_passed', 'needs_response'];
15      protected $dates = ['communication_date'];
16  
17      public function task()
18      {
19          return $this->belongsTo(Task::class);
20      }
21  
22      public function head()
23      {
24          return $this->belongsTo(Revision::class);
25      }
26  
27      public function createdBy()
28      {
29          return $this->belongsTo(User::class, 'created_by');
30      }
31  
32      public function sendToEntitable(): MorphTo
33      {
34          return $this->morphTo();
35      }
36  
37      public function getRecipientsNames()
38      {
39          return implode(', ', $this->users->pluck('name')->toArray());
40      }
41  
42      public function getDocumentsNames(): string
43      {
44          return implode(', ', $this->revisions->map(function ($revision) {
45              return $revision->document;
46          })->pluck('name')->toArray());
47      }
48  
49      public function dossier()
50      {
51          return $this->belongsTo(Dossier::class);
52      }
53  
54      public function users(): MorphToMany
55      {
56          return $this->morphedByMany(User::class, 'outputable');
57      }
58  
59      public function documents(): MorphToMany
60      {
61          return $this->morphedByMany(Document::class, 'outputable');
62      }
63  
64      public function revisions(): MorphToMany
65      {
66          return $this->morphedByMany(Revision::class, 'outputable');
67      }
68  }