123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. class Esg extends Model
  10. {
  11. use HasFactory, SoftDeletes, HasTranslations;
  12. //
  13. protected $guarded = ["id"];
  14. protected $casts = [
  15. 'on_top' => 'boolean'
  16. ];
  17. protected $appends = ["banner_pc_url", "banner_mobile_url"];
  18. public $translatable = ['title', 'banner_alt', 'description'];
  19. public function paragraphs(){
  20. return $this->hasMany(EsgParagraph::class)->orderBy('order');
  21. }
  22. protected function bannerPcUrl(): Attribute
  23. {
  24. return Attribute::make(
  25. get: fn ($value) => is_null($this->banner_pc) ? null :Storage::disk('public')->url($this->banner_pc),
  26. );
  27. }
  28. protected function bannerMobileUrl(): Attribute
  29. {
  30. return Attribute::make(
  31. get: fn ($value) => is_null($this->banner_mobile) ? null :Storage::disk('public')->url($this->banner_mobile),
  32. );
  33. }
  34. protected function getContentByKeyword($keyword){
  35. return $this->where("keyword", $keyword)->with('paragraphs')->first();
  36. }
  37. }