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
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
...
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With