NewsParagraph.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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_video_img', 'paragraph_video_type', 'paragraph_video_url'];
  19. public $translatable = ['text_content', 'video_img_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. public function photos(){
  37. return $this->hasMany(NewsPhoto::class)->orderBy("order");
  38. }
  39. protected function paragraphVideoType(): Attribute
  40. {
  41. return Attribute::make(
  42. get: fn ($value) => ($this->paragraph_type == 3) ? ($this->attributes["video_type"] == 1 ? "url" : "upload") : null,
  43. );
  44. }
  45. protected function paragraphVideoImg(): Attribute
  46. {
  47. return Attribute::make(
  48. get: fn ($value) => is_null($this->video_img) ? null :Storage::disk('public')->url($this->video_img),
  49. );
  50. }
  51. protected function paragraphVideoUrl(): Attribute
  52. {
  53. return Attribute::make(
  54. get: fn ($value) => ($this->attributes["video_type"] == 2) ? Storage::disk('public')->url($this->attributes["video_url"]) : $this->attributes["link"],
  55. );
  56. }
  57. }