47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Veelasky\LaravelHashId\Eloquent\HashableId;
|
|
|
|
class Membership extends Model
|
|
{
|
|
use HashableId, SoftDeletes;
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'total_spending' => 'int',
|
|
'reward_points' => 'int',
|
|
];
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function tier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tier::class);
|
|
}
|
|
|
|
#[Scope]
|
|
protected function scopeForSpending(Builder $query, $totalSpending): void
|
|
{
|
|
$query
|
|
->where('min_spending', '<=', $totalSpending)
|
|
->where(function ($q) use ($totalSpending) {
|
|
$q->whereNull('max_spending')
|
|
->orWhere('max_spending', '>=', $totalSpending);
|
|
});
|
|
}
|
|
}
|