GoodsModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // 封面图
  23. public function getThumbAttribute($value)
  24. {
  25. $thumb = $value ? get_image_url($value) : get_image_url('/images/goods/goods.jpeg');
  26. return $thumb;
  27. }
  28. public function setThumbAttribute($value)
  29. {
  30. return $value ? get_image_path($value) : '';
  31. }
  32. public function getPriceAttribute($value)
  33. {
  34. return $value ? floatval($value) : '0.00';
  35. }
  36. public function getMarketPriceAttribute($value)
  37. {
  38. return $value ? floatval($value) : '0.00';
  39. }
  40. /**
  41. * 图册
  42. * @param $value
  43. * @return array|string
  44. */
  45. public function getAlbumsAttribute($value)
  46. {
  47. $value = $value? get_images_preview($value,'url',2) : [];
  48. return $value;
  49. }
  50. public function getTypeNameAttribute()
  51. {
  52. $types = config('platform.goodsTypes');
  53. return isset($types[$this->type]) ? $types[$this->type] : '';
  54. }
  55. public function getInfo($id)
  56. {
  57. $info = parent::getInfo($id); // TODO: Change the autogenerated stub
  58. return $info;
  59. }
  60. /**
  61. * 关联商品分类
  62. */
  63. public function category()
  64. {
  65. return $this->hasOne(GoodsCategoryModel::class, 'id', 'category_id')
  66. ->select(['id', 'name', 'icon', 'status']);
  67. }
  68. /**
  69. * SKU
  70. */
  71. public function sku()
  72. {
  73. return $this->hasOne(GoodsSkuModel::class, 'id', 'sku_id')
  74. ->where(['status' => 1,'mark' => 1])
  75. ->orderBy('sort', 'desc');
  76. }
  77. /**
  78. * 关联商品规格(多规格)
  79. */
  80. public function skus()
  81. {
  82. return $this->hasMany(GoodsSkuModel::class, 'goods_id', 'id')
  83. ->where(['status' => 1,'mark' => 1])
  84. ->orderBy('sort', 'desc');
  85. }
  86. }