Attr.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php namespace App\Models\Product;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 产品属性
  6. *
  7. */
  8. class Attr extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'product_attr';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 如果操作失败
  30. if( !$id ) return $id;
  31. // 返回结果
  32. return $id;
  33. }
  34. /**
  35. * 添加数据
  36. *
  37. */
  38. public function edit($id,$data)
  39. {
  40. // 更新时间
  41. $data['update_time'] = time();
  42. // 写入数据表
  43. $result = $this->query()->where([['id','=',$id]])->update($data);
  44. // 如果操作失败
  45. if( !$result ) return $result;
  46. // 返回结果
  47. return $result;
  48. }
  49. /**
  50. * 添加数据
  51. *
  52. */
  53. public function upsertByName($name,$productId,$specId)
  54. {
  55. // 写入数据表
  56. $id = $this->query()->where([['name','=',$name],['product_id','=',$productId],['spec_id','=',$specId]])->value('id');
  57. // 如果操作失败
  58. if( !$id ) return $this->add(['name'=>$name,'product_id'=>$productId,'spec_id'=>$specId]);
  59. // 返回结果
  60. return $id;
  61. }
  62. /**
  63. * 添加数据
  64. *
  65. */
  66. public function getListByProductId($productId)
  67. {
  68. // 写入数据表
  69. $list = $this->query()->where([['product_id','=',$productId]])->get(['id','name','spec_id','product_id','thumb','remark'])->toArray();
  70. // 返回结果
  71. return $list;
  72. }
  73. /**
  74. * 添加数据
  75. *
  76. */
  77. public function getValueById($id,$field='')
  78. {
  79. // 写入数据表
  80. $result = $this->query()->where([['id','=',$id]])->value($field);
  81. // 返回结果
  82. return $result;
  83. }
  84. }