CustomRedpacket.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 客户红包
  6. *
  7. */
  8. class CustomRedpacket extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'custom_redpacket';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 编码转id
  20. *
  21. * @param string $code 编码
  22. *
  23. */
  24. public function codeToId($code){
  25. return intval(str_ireplace('klhb','',$code));
  26. }
  27. /**
  28. * id转编码
  29. *
  30. * @param int $id 编码
  31. *
  32. */
  33. public function idToCode($id){
  34. return 'klhb'. str_pad($id, 9, '0', STR_PAD_LEFT);
  35. }
  36. /**
  37. * 添加数据
  38. *
  39. */
  40. public function add($data)
  41. {
  42. // 时间
  43. $data['insert_time'] = time();
  44. $data['update_time'] = time();
  45. // 写入数据表
  46. $id = $this->query()->insertGetId($data);
  47. // 返回结果
  48. return $id;
  49. }
  50. /**
  51. * 添加数据
  52. *
  53. */
  54. public function edit($id,$data)
  55. {
  56. // 更新时间
  57. $data['update_time'] = time();
  58. // 写入数据表
  59. $result = $this->query()->where(['id'=>$id])->update($data);
  60. // 返回结果
  61. return $result;
  62. }
  63. /**
  64. * 查询某条数据
  65. *
  66. */
  67. public function getOne($uid){
  68. // 返回结果
  69. $result = $this->query()->where([['custom_uid','=',$uid]])->first();
  70. // 返回结果
  71. return $result;
  72. }
  73. }