NewsParagraph.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Illuminate\Support\Facades\Storage;
  8. use Spatie\Translatable\HasTranslations;
  9. use App\Models\News;
  10. class NewsParagraph extends Model
  11. {
  12. use HasFactory, HasTranslations;
  13. CONST IMAGE = 1;
  14. CONST TEXT = 2;
  15. CONST VIDEO = 3;
  16. protected $guarded = ['id'];
  17. public $timestamps = false;
  18. protected $appends = ['paragraph_img', 'paragraph_video_img', 'paragraph_video_type', 'paragraph_video_url'];
  19. public $translatable = ['text_content', 'image_alt'];
  20. public function news(){
  21. return $this->belongsTo(News::class);
  22. }
  23. public function contentType()
  24. {
  25. switch ($this->paragraph_type){
  26. case 1:
  27. return "image";
  28. case 2:
  29. return "text";
  30. case 3:
  31. return "video";
  32. default:
  33. return "";
  34. }
  35. }
  36. protected function paragraphImg(): Attribute
  37. {
  38. return Attribute::make(
  39. get: fn ($value) => is_null($this->image_url) ? null :Storage::disk('public')->url($this->image_url),
  40. );
  41. }
  42. protected function paragraphVideoType(): Attribute
  43. {
  44. return Attribute::make(
  45. get: fn ($value) => ($this->paragraph_type == 3) ? ($this->attributes["video_type"] == 1 ? "url" : "upload") : null,
  46. );
  47. }
  48. protected function paragraphVideoImg(): Attribute
  49. {
  50. return Attribute::make(
  51. get: fn ($value) => is_null($this->video_img) ? null :Storage::disk('public')->url($this->video_img),
  52. );
  53. }
  54. protected function paragraphVideoUrl(): Attribute
  55. {
  56. return Attribute::make(
  57. get: fn ($value) => ($this->attributes["video_type"] == 2) ? Storage::disk('public')->url($this->attributes["video_url"]) : $this->attributes["link"],
  58. );
  59. }
  60. }