1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Models;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class Order extends Model{
- use SoftDeletes;
- protected $table='order';
- protected $guarded=[];
- public static function order_no($type)
- {
- if($type=='th'){
- $prefix = 'TH';
- }else{
- $prefix = 'DD';
- }
- $timestamp=date('ymdHis') . substr(microtime(), 2, 6);
- $rand=mt_rand(10000, 99999);
- $order_no=$prefix . $timestamp . $rand;
- if(self::where('order_no',$order_no)->first()){
- self::order_no();
- }else{
- return $order_no;
- }
- }
- public function orderDetail()
- {
- return $this->hasMany(OrderDetail::class,'order_no','order_no');
- }
- public function address()
- {
- return $this->hasOne(Address::class,'id','address_id')->withTrashed();
- }
- public function store(){
- return $this->hasOne(Store::class,'id','store_id')->withTrashed();
- }
- public function user(){
- return $this->hasOne(User::class,'id','user_id')->withTrashed();
- }
- public function cancel()
- {
- return $this->hasOne(OrderCancel::class,'order_id','id');
- }
- public function refund()
- {
- return $this->hasMany(OrderRefund::class,'order_no','order_no');
- }
- public function getAccountAttribute($value){
- return round($value);
- }
- public function getSnapshotAttribute($value){
- $val = json_decode($value, true);
- foreach($val as $k=>$v){
- $val[$k]['name'] = str_replace('健康', '', $v['name']);
- }
- return json_encode($val);
- }
- }
|