$sellItems * @property-read \App\Models\Customers|null $customer * @property-read \App\Models\DeliveryAddress|null $deliveryAddress * @package App\Models */ class Order extends Model { use CastsEnums; /** * The attributes that should be cast to Enums. * * @var array */ protected $enumCasts = [ 'civility' => ContactFormCivility::class, 'status' => OrderStatus::class, 'payementType' => OrderType::class ]; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'civility', 'lastname', 'firstname', 'address', 'postcode', 'city', 'email', 'phone' ]; // * RELATIONSHIPS /** * The sellItems that belong to the Order * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function sellItems() { return $this->belongsToMany(SellItem::class)->withPivot('quantity'); } /** * The customer that belong to the Order * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function customer() { return $this->belongsTo(Customers::class); } /** * Get the user associated with the Order * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function deliveryAddress() { return $this->hasOne(DeliveryAddress::class); } // * METHODS /** * Get all failed orders old than more one month * * @return \Illuminate\Database\Query\Builder */ public static function outdatedFailedOrders() { return Order::where('status', OrderStatus::failed()) ->where('payementType', OrderType::cb()) ->whereDate('updated_at', '<=', Carbon::now()->subMonth()->toDateString()); } /** * Export the model data to a Formatted Arcadia Data array * * @return array> */ public function exportToOrderMailData(): array { $sellItems = $this->sellItems()->withPivot('quantity')->get(); return [ 'civility' => (string)$this->civility, 'lastname' => $this->lastname, 'firstname' => $this->firstname, 'address' => $this->address, 'postcode' => $this->postcode, 'city' => $this->city, 'email' => $this->email, 'phone' => $this->phone, 'items' => $sellItems->toArray() ]; } }