27 lines
546 B
PHP
27 lines
546 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class Cart extends Model
|
|
{
|
|
protected $fillable = ['user_id', 'product_id', 'quantity'];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function getSubtotalAttribute(): float
|
|
{
|
|
return (float) $this->product->price * $this->quantity;
|
|
}
|
|
}
|