【发布时间】:2020-01-15 05:43:31
【问题描述】:
我有 2 款优惠券和产品。
一张优惠券可以有多个产品。
关键是它可能是一些不同的组合(例如productA + productB,或productA OR productB等......)
所以我所做的不是在优惠券和产品之间建立关系,而是将产品 ID 像字符串一样存储在我的字段中(例如:1+2,这意味着当购买这两种产品时,此优惠券可用)。
我承认这可能不是我最好的主意......
现在我的问题是我需要根据产品类别(我的产品模型中的一个字段)对我的优惠券提出请求。 是否可以进行这样的查询(我最初认为我可以使用 Coupon 上的范围来实现它,但我无法做到),或者我的数据库结构这样太糟糕了,我应该更新它并使用多态关系(我也不知道该怎么做)。
这是我的优惠券模型:
class Coupon extends Model {
use Traits\Uuids;
protected static function boot()
{
parent::boot();
static::addGlobalScope(function ($query) {
$query->hasProductCategory();
});
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'products_id',
'client_id',
'facial_value',
'image',
'is_valid',
'begin_date',
'end_date',
'use_case'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'created_at',
'updated_at'
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* We define our default primary key.
*
* @var string
*/
protected $primaryKey = 'uuid';
/**
* Define if coupon has multiple products or just one
* return bool
*/
public function scopeIsSingleProduct():bool
{
$aProduct = explode("+", $this->products);
return count($aProduct) > 1 ? false : true;
}
public function client()
{
return $this->belongsTo(Client::class, 'client_id');
}
public function cart()
{
return $this->hasMany(Cart::class);
}
public function campaign()
{
return $this->hasMany(Campaign::class);
}
这是产品的模型
class Product extends Model {
protected $fillable = [
'name',
'ean',
'category_id',
'client_id',
'init_price'
];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [
'created_at',
'updated_at'
];
public function client()
{
return $this->belongsTo(Client::class, 'client_id');
}
public function category()
{
return $this->belongsTo(CategoryProduct::class, 'category_id');
}
public function productsId()
{
return preg_split( "/(\+|\|)/", $this->products);
}
}
感谢您的帮助。
【问题讨论】:
-
您只需要使用多对多关系。没有多少关系
-
其实我的问题是我没有建立关系,我只是想知道我是否可以在查询中动态建立它。
-
更新了我的帖子以添加两个模型。
-
product和coupon之间存在多对多关系的问题是什么?为什么要使用字符串字段来保存多个 id,因为您可以拥有一个表,其中每个 id 都有一个条目(与数据透视表的多对多关系coupon_product) -
如上所述,我使用的是字符串字段,因为我没有简单的关系,我需要以某种方式存储这种复杂性。优惠券可以与多种组合一起使用(例如产品 A 或产品 B,但也可能是产品 A + 产品 B),我认为这种方式更容易管理这些 AND / OR 差异。