Redpacket.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php namespace App\Models\Custom;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 客户红包模型
  6. *
  7. */
  8. class Redpacket 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. * 添加数据
  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 0;
  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 0;
  46. // 返回结果
  47. return $id;
  48. }
  49. /**
  50. * 获取优惠券信息
  51. *
  52. */
  53. public function getOne($id,$field=''){
  54. // 返回结果
  55. $result = $this->query()->find($id);
  56. // 返回结果
  57. $result = $result ? $result->toArray() : [];
  58. // 返回值
  59. return empty($field) ? $result : ( isset($result[$field]) ? $result[$field] : null);
  60. }
  61. }