66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\IsShow;
|
|
use App\Enums\RewardCategory;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
|
use Veelasky\LaravelHashId\Eloquent\HashableId;
|
|
|
|
class Reward extends Model implements HasMedia
|
|
{
|
|
use HasFactory, HashableId, InteractsWithMedia, SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'points' => 'int',
|
|
'stock' => 'int',
|
|
'category' => RewardCategory::class,
|
|
'is_show' => IsShow::class,
|
|
];
|
|
}
|
|
|
|
#[Scope]
|
|
protected function show(Builder $query): void
|
|
{
|
|
$query->where('is_show', IsShow::SHOW);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function hide(Builder $query): void
|
|
{
|
|
$query->where('is_show', IsShow::HIDDEN);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function active(Builder $query): void
|
|
{
|
|
$query->whereDate('start_date', '<=', now())
|
|
->where(function ($q) {
|
|
$q->whereNull('end_date')
|
|
->orWhereDate('end_date', '>=', now());
|
|
});
|
|
}
|
|
|
|
public function tiers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Tier::class);
|
|
}
|
|
|
|
public function redemptions(): HasMany
|
|
{
|
|
return $this->hasMany(Redemption::class);
|
|
}
|
|
}
|