GoodsModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Models;
  12. /**
  13. * 商品管理-模型
  14. * @author laravel开发员
  15. * @since 2020/11/11
  16. * @package App\Models
  17. */
  18. class GoodsModel extends BaseModel
  19. {
  20. // 设置数据表
  21. protected $table = 'goods';
  22. protected $appends = ['type_name'];
  23. // 封面图
  24. public function getThumbAttribute($value)
  25. {
  26. $thumb = $value ? get_image_url($value) : get_image_url('/images/goods/goods.jpeg');
  27. return $thumb;
  28. }
  29. public function setThumbAttribute($value)
  30. {
  31. return $value ? get_image_path($value) : '';
  32. }
  33. public function getPriceAttribute($value)
  34. {
  35. return $value ? floatval($value) : '0.00';
  36. }
  37. public function getMarketPriceAttribute($value)
  38. {
  39. return $value ? floatval($value) : '0.00';
  40. }
  41. /**
  42. * 图册
  43. * @param $value
  44. * @return array|string
  45. */
  46. public function getAlbumsAttribute($value)
  47. {
  48. $value = $value? get_images_preview($value,'url',2) : [];
  49. return $value;
  50. }
  51. public function getTypeNameAttribute()
  52. {
  53. $types = config('platform.goodsTypes');
  54. return isset($types[$this->type]) ? $types[$this->type] : '';
  55. }
  56. public function getInfo($id)
  57. {
  58. $info = parent::getInfo($id); // TODO: Change the autogenerated stub
  59. return $info;
  60. }
  61. /**
  62. * 关联商品分类
  63. */
  64. public function category()
  65. {
  66. return $this->hasOne(GoodsCategoryModel::class, 'id', 'category_id')
  67. ->select(['id', 'name', 'icon', 'status']);
  68. }
  69. /**
  70. * SKU
  71. */
  72. public function sku()
  73. {
  74. return $this->hasOne(GoodsSkuModel::class, 'id', 'sku_id')
  75. ->where(['status' => 1,'mark' => 1])
  76. ->orderBy('sort', 'desc');
  77. }
  78. /**
  79. * 关联商品规格(多规格)
  80. */
  81. public function skus()
  82. {
  83. return $this->hasMany(GoodsSkuModel::class, 'goods_id', 'id')
  84. ->where(['status' => 1,'mark' => 1])
  85. ->orderBy('sort', 'desc');
  86. }
  87. }