Thormar

Model

Oct 7th, 2021
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4.  
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use App\Traits\DateFormattedModelTrait;
  8.  
  9. class Order extends Model
  10. {
  11.     use HasFactory;
  12.  
  13.     protected $table = 'orders';
  14.  
  15.     protected $appends = [
  16.         'same',
  17.         'prod_name'
  18.     ];
  19.  
  20.     protected $fillable = [
  21.         'id',
  22.         'date',
  23.         'auction_numb',
  24.         'customer_name',
  25.         'ebay_id',
  26.         'address',
  27.         'email',
  28.         'item',
  29.         'finish',
  30.         'prod_status',
  31.         'return_numb',
  32.         'payment',
  33.         'processing_order',
  34.         'sold_price',
  35.         'tax',
  36.         'shipping_cost',
  37.         'finish_rem',
  38.         'order_rem',
  39.         'shipping_rem',
  40.         'total',
  41.         'tracking_numb',
  42.         'sh_adress',
  43.         'refund',
  44.         'sh_name',
  45.         'supplier',
  46.         'subtotal',
  47.         'exchange_numb',
  48.         'position',
  49.         'phone',
  50.         'other_item',
  51.         'other_item_id',
  52.         'shipped_by',
  53.         'attention',
  54.         'qty',
  55.         'proc_time',
  56.         'ebayac',
  57.         'stockwithdraw',
  58.         'customer_id',
  59.     ];
  60.  
  61.     public function status() {
  62.         return $this->belongsTo(Status::class, 'prod_status', 'id');
  63.     }
  64.  
  65.     public function product() {
  66.         return $this->belongsTo(Item::class, 'item', 'id');
  67.     }
  68.  
  69.     public function scopeToday($query) {
  70.         return $query->where('created_at', '>=',today());
  71.     }
  72.  
  73.     public function scopeWarning($query) {
  74.         return $query->where('attention', 1);
  75.     }
  76.  
  77.     public function account() {
  78.         return $this->belongsTo(EbayAccount::class, 'ebayac', 'id');
  79.     }
  80.  
  81.     public function customer() {
  82.         return $this->belongsTo(Customer::class, 'customer_id', 'id');
  83.     }
  84.  
  85.     public function getSameAttribute(){
  86.         return $this->sh_adress === $this->address;
  87.     }
  88.  
  89.     public function getDateAttribute($value): string {
  90.         if (empty($value))
  91.             return '';
  92.         return \Carbon\Carbon::parse($value)->format('m/d/Y');
  93.     }
  94.  
  95.     public function setDateAttribute($date){
  96.         if (!empty($date)) {
  97.             $this->attributes['date'] = \Carbon\Carbon::parse($date)->format('Y-m-d H:i:s');
  98.         }
  99.     }
  100.  
  101.     public function getProdNameAttribute(){
  102.         $this->load('product');
  103.         if (is_null($this->item) || $this->item === 0) {
  104.             return $this->other_item;
  105.         }
  106.         if (is_null($this->product)) {
  107.             return null;
  108.         }
  109.         return $this->product->name;
  110.     }
  111.  
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment