Order.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }