GoodsModel.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /**
  33. * 图册
  34. * @param $value
  35. * @return array|string
  36. */
  37. public function getAlbumsAttribute($value)
  38. {
  39. $value = $value? explode(',',$value) : [];
  40. if ($value) {
  41. foreach ($value as &$album) {
  42. $album = get_image_url($album);
  43. }
  44. unset($album);
  45. }
  46. return $value;
  47. }
  48. public function getInfo($id)
  49. {
  50. $info = parent::getInfo($id); // TODO: Change the autogenerated stub
  51. return $info;
  52. }
  53. /**
  54. * 商家店铺
  55. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  56. */
  57. public function store()
  58. {
  59. return $this->hasOne(StoreModel::class, 'id', 'store_id')
  60. ->select(['id','nickname','mobile','bonus_rate','status']);
  61. }
  62. /**
  63. * 关联商品分类
  64. */
  65. public function category()
  66. {
  67. return $this->hasOne(GoodsCategoryModel::class, 'id', 'category_id')
  68. ->select(['id', 'name', 'icon', 'status']);
  69. }
  70. /**
  71. * 关联商品规格(多规格)
  72. */
  73. public function skus()
  74. {
  75. return $this->hasMany(GoodsSkuModel::class, 'goods_id', 'id')
  76. ->where(['mark' => 1, 'status' => 1])
  77. ->orderBy('sort', 'desc');
  78. }
  79. }