راکت

3 کلید خارجی در یک پیوت تیبل

1 پاسخ

سلام دوستان،واسه یه فروشگاه شبیه دیجی کالا،قسمت ویژگی های محصولات، روابط رو به این شکل نوشتم

class Attribute extends Model
{

    /**
     * relation attribute with category.
     * @return BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * relation attribute with product with attributeValue_id pivot.
     * @return BelongsToMany
     */
    public function products()
    {
        return $this->belongsToMany(Product::class)->withPivot('attributeValue_id');
    }

    /**
     * relation attribute with attributeValue_id with product_id pivot.
     * @return BelongsToMany
     */
    public function attributeValues()
    {
        return $this->belongsToMany(AttributeValue::class,'attribute_product','attribute_id','attributeValue_id')->withPivot('product_id');
    }
}

class AttributeValue extends Model
{

    /**
     * relation attributeValue with attribute with product_id pivot.
     * @return BelongsToMany
     */
    public function attributes()
    {
        return $this->belongsToMany(Attribute::class)->withPivot('product_id');
    }

    /**
     * relation attributeValue with product with attribute_id pivot.
     * @return BelongsToMany
     */
    public function product()
    {
        return $this->belongsToMany(Product::class)->withPivot('attribute_id');
    }
}

class Product extends Model
{
    /**
     * relation product with attribute with attributeValue_id pivot.
     * @return BelongsToMany
     */
    public function attributes()
    {
        return $this->belongsToMany(Attribute::class)->withPivot('attributeValue_id');
    }

    /**
     * relation product with AttributeValues with attribute_id pivot.
     * @return BelongsToMany
     */
    public function attributeValues()
    {
        return $this->belongsToMany(AttributeValue::class)->withPivot('attribute_id');
    }

}

ینی رابطه محصولات و ویژگی ها چند به چند هست و رابطه ویژگی ها با مقدار ویژگی با ویژگی یک به چند هست،ولی از همون چند به چند استفاده کردم،و هر سه کلید خارجی رو داخل یه پیوت قرار دادم، واسه ست کردن ویژگی و مقدار ویژگی به محصول مشکل دارم،

از این استفاده کردم ولی کامل مشکل رو حل نمیکنه

foreach ($request->attribute_id as $attribute => $attributeValue) {
            Attribute::whereHas('attributeValues', function ($query) use ($attributeValue) {
                $query->where('attributeValue_id', $attributeValue);
            })->find($attribute)->attributeValues()->updateExistingPivot($attributeValue, ['product_id' => $product->id]);
        }

دوستان کسی راه کاری داره؟ممنون میشم کمک کنید

ثبت پرسش جدید

پاسخ‌ها

1
mason hows
mason hows
@mason.hows11

اگه مشکلتو حل کردی که هیچ اگه نه پیام بذار یک توضیح بهم بده در مورد روابط بین جداولت ببینم میتونم کمکت کنم.

شاید پاسخ شما همین‌جا باشد

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام