Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel中的约束关系_Php_Mysql_Laravel_Laravel 7 - Fatal编程技术网

Php Laravel中的约束关系

Php Laravel中的约束关系,php,mysql,laravel,laravel-7,Php,Mysql,Laravel,Laravel 7,也许你能帮我解决我现在的问题 我有产品那有很多关系 /** Inherits from : category, subcategory and model */ Product->hasMany(CategoryAttributes, $this->category) Product->hasMany(SubCategoryAttributes, $this->subCategory) Product->hasMany(ModelAttributes, $this-

也许你能帮我解决我现在的问题

我有
产品
那有很多关系

/** Inherits from : category, subcategory and model */
Product->hasMany(CategoryAttributes, $this->category)
Product->hasMany(SubCategoryAttributes, $this->subCategory)
Product->hasMany(ModelAttributes, $this->model)

/** */
Product->hasMany(HideForSubcategoryAttributes, $this->subCategory)
Product->hasMany(IgnoreForModelAttributes, $this->model)
现在我需要实现一些东西,为我提供产品的所有属性

  • 收集模型、子类别和类别的属性
  • 过滤这些属性并拒绝应该忽略或隐藏的属性
  • 我很想通过使用某种拉威尔关系来实现它,我觉得这是可能的,但我不明白

    另一个解决方案是我使用原始的MySql查询,我的方法是使用子查询

    不幸的是,更改数据库不是一个选项,因为
    产品的这些属性应该是动态的。此外,它应该以某种方式优化性能和节省内存,因为我可以获得超过10k的产品,大约1k个型号,100个子类别和类别

    Product.php

    class Product extends Model
    {
        public $fillable = [
            'model_id',
            'subcategory_id',
            'category_id',
        ];
    
        /**
         * @return HasMany
         */
        public function categoryAttributes(): HasMany
        {
            return $this->hasMany(
                CategoryAttributes::class,
                'category_id',
                'category_id'
            );
        }
       /**
         * @return HasMany
         */
        public function hideSubcategoryAttributes(): HasMany
        {
            return $this->hasMany(
                HideForSubcategoryAttributes::class,
                'subcategory_id',
                'subcategory_id'
            );
        }
    
        /**
         * @return HasMany
         */
        public function subcategoryAttributes(): HasMany
        {
            return $this->hasMany(
                SubcategoryAttributes::class,
                'subcategory_id',
                'subcategory_id'
            );
        }
     /**
         * @return HasMany
         */
        public function ignoreForModelAttributes(): HasMany
        {
            return $this->hasMany(
                ModelIgnore::class,
                'model_id',
                'model_id'
            );
        }
    
        /**
         * @return HasMany
         */
        public function modelAttributes(): HasMany
        {
            return $this->hasMany(
                ModelAttribute::class,
                'model_id',
                'model_id'
    
            );
        }
    }
    
    
    Category.php、subcategory attributes.php、modeldattributes.php、HideForSubcategoryAttributes.php、ModelIgnore.php都有以下关系:

    
        public function attribute(): HasOne
        {
            return $this->hasOne(Attribute::class, 'id', 'attribute_id');
        }
    
    在某个地方,我希望所有属性都是有效的(不被忽略和隐藏),因此我需要以下各项的总和:


    (类别+子类别属性+模型属性)-(HideForSubcategoryAttributes+ModelIgnore)=验证产品属性

    问题不清楚,请提供模型详细信息/代码。