Order.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. class Order extends Model{
  7. use SoftDeletes;
  8. protected $table='order';
  9. protected $guarded=[];
  10. public static function order_no($type)
  11. {
  12. if($type=='th'){
  13. $prefix = 'TH';
  14. }else{
  15. $prefix = 'DD';
  16. }
  17. $timestamp=date('ymdHis') . substr(microtime(), 2, 6);
  18. $rand=mt_rand(10000, 99999);
  19. $order_no=$prefix . $timestamp . $rand;
  20. if(self::where('order_no',$order_no)->first()){
  21. self::order_no();
  22. }else{
  23. return $order_no;
  24. }
  25. }
  26. public function orderDetail()
  27. {
  28. return $this->hasMany(OrderDetail::class,'order_no','order_no');
  29. }
  30. public function address()
  31. {
  32. return $this->hasOne(Address::class,'id','address_id')->withTrashed();
  33. }
  34. public function store(){
  35. return $this->hasOne(Store::class,'id','store_id')->withTrashed();
  36. }
  37. public function user(){
  38. return $this->hasOne(User::class,'id','user_id')->withTrashed();
  39. }
  40. public function cancel()
  41. {
  42. return $this->hasOne(OrderCancel::class,'order_id','id');
  43. }
  44. public function refund()
  45. {
  46. return $this->hasMany(OrderRefund::class,'order_no','order_no');
  47. }
  48. public function getAccountAttribute($value){
  49. return round($value);
  50. }
  51. public function getSnapshotAttribute($value){
  52. $val = json_decode($value, true);
  53. foreach($val as $k=>$v){
  54. $val[$k]['name'] = str_replace('健康', '', $v['name']);
  55. }
  56. return json_encode($val);
  57. }
  58. }