66 lines
1.2 KiB
PHP
66 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class CouponRange extends Model
|
|
{
|
|
use HasFactory;
|
|
use HasDateTimeFormatter;
|
|
|
|
public const TYPE_CATEGORY = 1;
|
|
public const TYPE_PRODUCT = 2;
|
|
|
|
/**
|
|
* 仅查询可用的券规则
|
|
*/
|
|
public function scopeIsEnable($query)
|
|
{
|
|
return $query->where('is_enable', true);
|
|
}
|
|
|
|
public function coupon()
|
|
{
|
|
return $this->belongsTo(Coupon::class, 'coupon_id');
|
|
}
|
|
|
|
/**
|
|
* 确认券规则是否仅分类可用
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isTypeCategory()
|
|
{
|
|
return $this->type === static::TYPE_CATEGORY;
|
|
}
|
|
|
|
/**
|
|
* 确认券规则是否仅商品可用
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isTypeProduct()
|
|
{
|
|
return $this->type === static::TYPE_PRODUCT;
|
|
}
|
|
|
|
/**
|
|
* 获取此规则的范围IDs
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getRangeIdsAttribute(): array
|
|
{
|
|
$value = $this->attributes['ranges'];
|
|
|
|
if ((string) $value === '') {
|
|
return [];
|
|
}
|
|
|
|
return explode(',', $value);
|
|
}
|
|
}
|