海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Support\Facades\Storage;
  6. /**
  7. * 供应商产品
  8. * Class Product
  9. * @package App\Models
  10. */
  11. class Product extends BaseModel
  12. {
  13. use HasFactory, SoftDeletes;
  14. protected $guarded = ['id']; //不允许编辑的字段
  15. protected $appends = ['picture'];
  16. // 获取所有产品图片
  17. public function getPicturesAttribute($value): array
  18. {
  19. $pictures = $value ? json_decode($value, true) : [];
  20. foreach ($pictures as &$v) {
  21. $v = Storage::disk('public')->url($v);
  22. }
  23. $this->append('picture');
  24. return $pictures;
  25. }
  26. // 获取第一张产品图片
  27. public function getPictureAttribute($value): string
  28. {
  29. return $this->pictures[0] ?? '';
  30. }
  31. //保存图片时去掉域名
  32. public function setPicturesAttribute($value)
  33. {
  34. if (is_array($value)) {
  35. foreach ($value as &$v) {
  36. $v = str_replace(Storage::disk('public')->url(''), '', $v);
  37. }
  38. $this->attributes['pictures'] = json_encode($value);
  39. }
  40. }
  41. public function supplier()
  42. {
  43. return $this->belongsTo(Supplier::class);
  44. }
  45. public function category()
  46. {
  47. return $this->belongsTo(Category::class);
  48. }
  49. }