ekart/backend/app/Models/Order.php

56 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* @mixin IdeHelperOrder
*/
class Order extends Model
{
public $fillable = [
'user_id', 'cart_id', 'status', 'shipping_city', 'shipping_street', 'shipping_last_name', 'shipping_first_name',
'shipping_state', 'shipping_pin',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function cart(): BelongsTo
{
return $this->belongsTo(Cart::class);
}
/**
* Stripe session id helps to update status later from webhook
*
* @return HasOne<StripeSession>
*/
public function stripeSession(): HasOne
{
return $this->hasOne(StripeSession::class);
}
/**
* @return HasMany<Payment>
*/
public function payments(): HasMany
{
return $this->hasMany(Payment::class);
}
protected function totalAmount(): Attribute
{
return Attribute::make(
fn () => $this->cart?->products->sum(fn ($product) => $product->pivot->price * $product->pivot->quantity) ?? 0
);
}
}