OrdersProduct.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php namespace App\Models;
  2. use App\Models\Traits\MultipUpdate;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. /**
  6. * 子订单模型
  7. *
  8. */
  9. class OrdersProduct extends Model
  10. {
  11. use HasFactory,MultipUpdate;
  12. // 与模型关联的表名
  13. protected $table = 'orders_product';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. $data['insert_time'] = time();
  27. $data['update_time'] = time();
  28. // 写入数据表
  29. $id = $this->query()->insertGetId($data);
  30. // 返回结果
  31. return $id;
  32. }
  33. /**
  34. * 添加数据
  35. *
  36. */
  37. public function edit($id,$data)
  38. {
  39. // 更新时间
  40. $data['update_time'] = time();
  41. // 写入数据表
  42. $result = $this->query()->where(['id'=>$id])->update($data);
  43. // 返回结果
  44. return $result;
  45. }
  46. /**
  47. * 编码转id
  48. *
  49. * @param string $code 编码
  50. *
  51. */
  52. public function codeToId($code){
  53. return intval(str_ireplace('kldd','',$code));
  54. }
  55. /**
  56. * id转编码
  57. *
  58. * @param int $id 编码
  59. *
  60. */
  61. public function idToCode($id){
  62. return 'kldd'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  63. }
  64. /**
  65. * 获取订单产品数据
  66. *
  67. * @param Array ID
  68. * @param String 指定字段
  69. *
  70. */
  71. public function getOne($id,$field='')
  72. {
  73. // 获取列表数据
  74. $list = $this->getList();
  75. // 获取数据
  76. $one = isset($list[$id]) ? $list[$id] : [];
  77. // 返回值
  78. return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
  79. }
  80. }