Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel multiple many to many

Tags:

I am using many to many relation. i want to set two value for this attribute.

product,

attributes,

attribute_product => product_id,attribute_id,value

enter image description here

I know it's wrong , but I want to show you that i want

$product->attributes()->sync([
            1 => [
                'value' => 'sky'
            ],
            1 => [
                'value' => 'night'
            ],
        ]);

Update 2

Schema::create('attribute_product', function (Blueprint $table) {

$table->unsignedInteger('product_id');
$table->unsignedInteger('attribute_id');
$table->text('value')->nullable();
$table->integer('devalue_id')->nullable(); // value id 

$table->primary(['product_id', 'attribute_id', 'devalue_id']);

});

Update 1

I need to set sky,night

product_id  attribute_id      value       devalue_id
1           1                 sky         1
1           1                 night       2
...
like image 296
danial dezfooli Avatar asked Jun 29 '17 12:06

danial dezfooli


People also ask

What is Many to Many relationship in Laravel?

Many to many relationship is a little bit complicated than one to one and one to many relationships. An example of such a relationship is a user with may have multiple roles, where the role are also connected with multiple users. many to many relationship in laravel 6, laravel 7, laravel 8 and laravel 9.

What is polymorphic relationship in Laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.

How to Define relationship in Laravel?

To define a relationship, we need first to define the post() method in User model. In the post() method, we need to implement the hasOne() method that returns the result. Let's understand the one to one relationship through an example. First, we add the new column (user_id) in an existing table named as posts.


1 Answers

For something like this I can see 2 options:

1.

Manually attach and detach the records that you want. This may lead to issues with how you retrieve the data though as it will return multiple of the same attribute, however, you could potentially use groupBy on the collection to get around this issue (if it is one for you).

2.

You could store a JSON object in the value column on the pivot table. This would allow you to have multiple values for the same attribute. To do this you can create an AttributeProductPivot class (or whatever you want to call it) that extends Illuminate\Database\Eloquent\Relations\Pivot and add a casts property to it i.e.:

class AttributeProductPivot extends \Illuminate\Database\Eloquent\Relations\Pivot
{
    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'value' => 'collection', //or object or array
    ];

}

Then change your attributes() relationship to be:

public function attributes()
{
    return $this->belongsToMany(Attribute::class)
        ->using(AttributeProductPivot::class)
        ->withPivot('value');
}

The using() allows you to set a different Pivot Model that the default one. Don't forget to do the same for the inverse relationship if you need to.

You would need to cast the value attribute to json yourself when attaching/syncing.

Hope this helps!

like image 190
Rwd Avatar answered Oct 13 '22 17:10

Rwd