Laravel 雄辩-在查询中动态创建关系的可能性

Laravel 雄辩-在查询中动态创建关系的可能性,laravel,eloquent,lumen,Laravel,Eloquent,Lumen,我有两种型号的优惠券和产品 一张优惠券可以有几种产品 关键是它可能是一些不同的组合(例如productA+productB,或者productA或productB,等等) 因此,我所做的不是在优惠券和产品之间建立关系,而是将产品ID像字符串一样存储在我的字段中(例如:1+2,这意味着当购买两种产品时,此优惠券可用) 也许不是我最好的主意我承认 现在我的问题是,我需要根据产品类别(我的产品模型中的一个字段)对优惠券提出请求。 是否有可能进行这样的查询(我最初认为我可以通过优惠券上的作用域来实现它,

我有两种型号的优惠券和产品

一张优惠券可以有几种产品

关键是它可能是一些不同的组合(例如productA+productB,或者productA或productB,等等)

因此,我所做的不是在优惠券和产品之间建立关系,而是将产品ID像字符串一样存储在我的字段中(例如:1+2,这意味着当购买两种产品时,此优惠券可用)

也许不是我最好的主意我承认

现在我的问题是,我需要根据产品类别(我的产品模型中的一个字段)对优惠券提出请求。 是否有可能进行这样的查询(我最初认为我可以通过优惠券上的作用域来实现它,但我无法实现),或者我的数据库结构太糟糕了,我应该更新它并使用多态关系(不确定如何实现)

这是我的优惠券模型:

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);
}
}


谢谢您的帮助。

您应该重新考虑您的数据库结构

优惠券:产品是多对多关系


这意味着它们将存储在数据透视表中,您可以根据需要进行查询。此外,如果pivot表有助于您更轻松地编写查询,您可以为它创建一个模型。

您只需要使用多对多关系。没有太多的关系实际上我的问题是我没有建立关系,我只是想知道我是否可以在查询中动态地建立关系。更新了我的帖子以添加这两种模型。
产品
优惠券
之间存在多对多关系的问题是什么?为什么要使用字符串字段来保存多个id?当您可以有一个表,其中每个id都有一个条目(与透视表的多对多关系<代码>优惠券\产品),如上所述,我使用字符串字段是因为我没有简单的关系,我需要以某种方式存储这种复杂性。优惠券可以用于多种组合(例如productA或product B,但也可以是productA+productB),我认为这种方法更容易管理这些和/或差异。好的,我希望有一种方法可以模拟关系并保持我的实际结构。但我知道这很奇怪。经过一段时间的努力,我认为我确实需要更新我的数据库结构并使用透视表。我必须考虑一下它的结构,因为它不是一个“简单的”多对多关系,因为我必须管理多个产品和优惠券之间的不同类型的关系。谢谢你的回答。