DocumentationDocument.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. class DocumentationDocument extends Model
  10. {
  11. use HasFactory;
  12. protected $fillable = [
  13. 'folder_id',
  14. 'name',
  15. 'last_version_number',
  16. 'created_by',
  17. ];
  18. protected function casts(): array
  19. {
  20. return [
  21. 'last_version_number' => 'integer',
  22. ];
  23. }
  24. public function folder(): BelongsTo
  25. {
  26. return $this->belongsTo(DocumentationFolder::class, 'folder_id');
  27. }
  28. public function creator(): BelongsTo
  29. {
  30. return $this->belongsTo(User::class, 'created_by');
  31. }
  32. public function versions(): HasMany
  33. {
  34. return $this->hasMany(DocumentationDocumentVersion::class, 'document_id')
  35. ->orderByDesc('version');
  36. }
  37. public function currentVersion(): HasOne
  38. {
  39. return $this->hasOne(DocumentationDocumentVersion::class, 'document_id')
  40. ->ofMany('version', 'max');
  41. }
  42. }