海南旅游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.

51 lines
1.0 KiB

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