Level 41
You can use the sync() method and tell it not to detach anything.
$this->user->pages()->sync($data_ids, false);
This will attach only new data ids.
1 like
Hi,
is it possible with eloquent in a many-to-many relationship to mass attach a bunch of ids and ignore those already attached ?
do {
$data_ids = array();
foreach($jsonArray as $json) {
$page = \App\Page::createOrUpdatePage($json);
array_push($data_ids, $page->id);
}
$this->user->pages()->attach($data_ids);
}
while ($edge = $this->fb->next($edge));
In this case, if $this->user->pages() already contains some ids of $data_ids, i get an SQL error :
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' (SQL: insert into `page_user`
You can use the sync() method and tell it not to detach anything.
$this->user->pages()->sync($data_ids, false);
This will attach only new data ids.
Please or to participate in this conversation.