ShopCart.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 购物车模型
  7. *
  8. */
  9. class ShopCart extends Model
  10. {
  11. use HasFactory;
  12. // 与模型关联的表名
  13. protected $table = 'shop_cart';
  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,$uid,$data)
  38. {
  39. // 更新时间
  40. $data['update_time'] = time();
  41. // 写入数据表
  42. $result = $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->update($data);
  43. // 返回结果
  44. return $result;
  45. }
  46. /**
  47. * 添加数据
  48. *
  49. */
  50. public function del($id,$uid)
  51. {
  52. // 写入数据表
  53. $result = $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->delete();
  54. // 返回结果
  55. return $result;
  56. }
  57. /**
  58. * 数量更新
  59. *
  60. * @param int $id 购物车ID
  61. * @param int $uid 用户ID
  62. * @param int $incr 增减数量
  63. */
  64. public function incrBuyNum($id,$uid,$incr=1){
  65. // 返回结果
  66. return $this->query()->where([['id','=',$id],['custom_uid','=',$uid]])->update(['buy_num'=>DB::raw('buy_num+'.$incr),'update_time'=>time()]);
  67. }
  68. }