Files
getgreen-backend/app/Models/OrderProducts.php

67 lines
1.4 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @package App\Models
* @property integer $id
* @property integer $order_id
* @property integer $product_id
* @property integer $count
* @property $price
* @property integer $discount
*
* @property-read Order $order
* @property-read Product $product
*
* @property-read float $total_price
* @property-read float $final_price
*/
class OrderProducts extends Model
{
protected $guarded = ['id'];
public $timestamps = false;
protected $casts = [
'order_id' => 'integer',
'product_id' => 'integer',
'count' => 'integer',
];
public function order(): BelongsTo
{
return $this->belongsTo(Order::class)->withTrashed();
}
public function getCount()
{
return $this->count;
}
public function product(): BelongsTo
{
return $this->belongsTo(Product::class)->withTrashed();
}
public function getFinalPriceAttribute(): float
{
if (!empty($this->discount)) {
return $this->discount;
} else {
return $this->price;
}
}
public function getTotalPriceAttribute(): float
{
$price = $this->discount ?? $this->price;
return $price * $this->count;
}
}