62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\CartStatus;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @mixin IdeHelperCart
|
|
*/
|
|
class Cart extends Model
|
|
{
|
|
protected $fillable = ['user_id', 'status'];
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'status' => CartStatus::class,
|
|
];
|
|
}
|
|
|
|
public function products(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Product::class)
|
|
->withPivot('price', 'quantity')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function order(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Order::class);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query)
|
|
{
|
|
return $query->where('status', CartStatus::Active);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function withProducts(Builder $query)
|
|
{
|
|
return $query->with(['products' => function ($product) {
|
|
$product->withPivot('quantity', 'price');
|
|
}]);
|
|
}
|
|
}
|