Reputation: 85
I have used laravel sync
mthod for belongsToMany
relationship. After calling this method, sometimes duplicate data is inserted to the pivot table.
This the code
I have used.
{
$selectedScreenCategories = $this->screenCategorySiteFieldsModel->addSameKeyForElementsInArray($screenCategoryIds,'screen_category_id');
$siteField->screenCategories()->sync($selectedScreenCategories);
}
This is the migration
which is used to create pivot
table.
{
{
public function up()
{
Schema::create('site_field_screen_categories',function (Blueprint $table){
$table->increments('id');
$table->unsignedInteger('screen_category_site_field_id')->nullable(false);
$table->unsignedInteger('screen_category_id')->nullable(false);
$table->foreign('screen_category_site_field_id','screen_category_site_field_id')->references('id')->on('screen_category_site_fields');
$table->foreign('screen_category_id')->references('id')->on('screen_categories');
});
}
}
}
This is the belongsToMany
relationship.
function screenCategories(){
return $this->belongsToMany('App\ScreenCategory','site_field_screen_categories','screen_category_site_field_id','screen_category_id');
}
This is the pivot
table before
calling the sync
method.
This is the sync
array for $selectedScreenCategories
.
[
0 => ["screen_category_id" => 6]
1 => ["screen_category_id" => 3]
2 => ["screen_category_id" => 5]
]
This is the pivot
table after
calling sync
method.
I spent several hours. Still could not able to find the solution.
Upvotes: 1
Views: 3514
Reputation: 615
Solution.
Just change the integer keys
in to string keys
of the array $selectedScreenCategories
which is passed in to sync
function like below.
Change
[
0 => ["screen_category_id" => 6]
1 => ["screen_category_id" => 3]
2 => ["screen_category_id" => 5]
]
Into
[
"Gfdsj" => ["screen_category_id" => 6]
"pQrst" => ["screen_category_id" => 3]
"uvWzy" => ["screen_category_id" => 5]
]
You can use str_random(5)
php function to make random strings like "Gfdsj"
. This will fix your issue.
Reason for the issue.
Check the already existing record of the pivot
table before run the sync
function.
That record for screen_category_id = 1
.
Then check the array $selectedScreenCategories
which is passed to sync
function. See the image below.
When we pass this array to sync
function, because there is a record for screen_category_id = 1
already, above highlighted record is the reason to getting updated the already existing record as screen_category_id = 3
. Now we have 2 records for screen_category_id = 3
.
Hope you will understand.
Upvotes: 8
Reputation: 2972
If you want the combination to be unique in your pivot table, remove the id
from pivot table. In order to stop duplicate record you need to make unique key. In your current table id
is the primary key and incremental.
You can remove the id
and create composite primary key like
$table->primary(['screen_category_site_field_id', 'screen_category_id']);
Above will really enforce unique data.
Alternative is you can keep the table as it is and detach the data first and sync again.
Upvotes: 1