City.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php namespace App\Models\Product;
  2. use App\Models\Traits\MultipUpdate;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. /**
  6. * 产品SKU
  7. *
  8. */
  9. class City extends Model
  10. {
  11. use HasFactory,MultipUpdate;
  12. // 与模型关联的表名
  13. protected $table = 'product_city';
  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. if( !$id ) return $id;
  32. // 返回结果
  33. return $id;
  34. }
  35. /**
  36. * 添加数据
  37. *
  38. */
  39. public function edit($id,$data)
  40. {
  41. // 更新时间
  42. $data['update_time'] = time();
  43. // 写入数据表
  44. $result = $this->query()->where([['id','=',$id]])->update($data);
  45. // 如果操作失败
  46. if( !$result ) return 0;
  47. // 返回结果
  48. return $id;
  49. }
  50. /**
  51. * 添加数据
  52. *
  53. */
  54. public function getListByProductId($productId)
  55. {
  56. // 写入数据表
  57. $list = $this->query()->where([['product_id','=',$productId]])->pluck('city_id')->toArray();
  58. // 返回结果
  59. return $list;
  60. }
  61. }