ProductImage.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Webkul\Product\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Str;
  6. use Webkul\Product\Contracts\ProductImage as ProductImageContract;
  7. class ProductImage extends Model implements ProductImageContract
  8. {
  9. /**
  10. * Timestamp.
  11. *
  12. * @var bool
  13. */
  14. public $timestamps = false;
  15. /**
  16. * Fillable.
  17. *
  18. * @var array
  19. */
  20. protected $fillable = [
  21. 'type',
  22. 'path',
  23. 'product_id',
  24. 'position',
  25. ];
  26. /**
  27. * The accessors to append to the model's array form.
  28. *
  29. * @var array
  30. */
  31. protected $appends = ['url'];
  32. /**
  33. * Get the product that owns the image.
  34. *
  35. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  36. */
  37. public function product()
  38. {
  39. return $this->belongsTo(ProductProxy::modelClass());
  40. }
  41. /**
  42. * Get image url for the product image.
  43. *
  44. * @return string
  45. */
  46. public function url()
  47. {
  48. // Imported product images may store a remote/absolute URL directly in
  49. // the `path` column; return it verbatim instead of prefixing the local
  50. // storage path.
  51. if (is_string($this->path) && Str::startsWith($this->path, ['http://', 'https://'])) {
  52. return $this->path;
  53. }
  54. return Storage::url($this->path);
  55. }
  56. /**
  57. * Get image url for the product image.
  58. *
  59. * @return string
  60. */
  61. public function getUrlAttribute()
  62. {
  63. return $this->url();
  64. }
  65. /**
  66. * Is custom attribute.
  67. *
  68. * @param string $key
  69. * @return bool
  70. */
  71. public function isCustomAttribute($attribute)
  72. {
  73. return $this->attribute_family->custom_attributes->pluck('code')->contains($attribute);
  74. }
  75. }