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

57 lines
1.1 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. protected $casts = ['extends' => 'json'];
  15. // 获取所有产品图片
  16. public function getPicturesAttribute($value): array
  17. {
  18. if (is_string($value)) {
  19. $value = $value ? json_decode($value, true) : [];
  20. }
  21. $this->append('picture');
  22. return $value ?? [];
  23. }
  24. // 获取第一张产品图片
  25. public function getPictureAttribute($value): string
  26. {
  27. return $this->pictures[0] ?? '';
  28. }
  29. public function setPicturesAttribute($value)
  30. {
  31. if (is_array($value)) {
  32. $this->attributes['pictures'] = json_encode(array_filter($value));
  33. }
  34. }
  35. public function supplier()
  36. {
  37. return $this->belongsTo(Supplier::class);
  38. }
  39. public function category()
  40. {
  41. return $this->belongsTo(Category::class);
  42. }
  43. public function spec()
  44. {
  45. return $this->hasMany(ProductSpec::class);
  46. }
  47. }