Skip to content

Commit ca562d4

Browse files
committed
Updated readme
1 parent 2a94e22 commit ca562d4

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $customer->contacts()->sync([
6565
]);
6666
```
6767

68-
The sync method accepts an array of data to place on the intermediate table. Any data that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the data in the given array will exist in the intermediate table:
68+
The sync method accepts an array of data to place on the intermediate table. Any data that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the data in the given array will exist in the intermediate table.
6969

7070
#### Syncing without deleting
7171

@@ -86,6 +86,30 @@ $customer->contacts()->sync([
8686
], false);
8787
```
8888

89+
#### Behaviour for IDs that are not part of the hasMany relation
90+
91+
If an ID in the related data does not exist or is not in the scope of the `hasMany` relation, the `sync` function will throw a `ModelNotFoundException`.
92+
It is possible to modify this behavior with the `$throwOnIdNotInScope` attribute. Per default, this is set to `true`. If set to false, the `sync` function will ignore the Ids instead of throwing an exception.
93+
94+
```php
95+
$customer->contacts()->sync([
96+
[
97+
'id' => 7, // ID that belongs to a different customer than `$customer`
98+
'name' => 'Peter',
99+
'phone_number' => '321',
100+
],
101+
[
102+
'id' => 1000, // ID that does not exist
103+
'name' => 'Alfa',
104+
'phone_number' => '123',
105+
],
106+
[
107+
'id' => null,
108+
'name' => 'Adhitya',
109+
'phone_number' => '234,
110+
]
111+
], throwOnIdNotInScope: false);
112+
```
89113

90114
#### Example usage in the controller.
91115

tests/HasManySyncTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\ModelNotFoundException;
9-
use Illuminate\Foundation\Testing\RefreshDatabase;
109
use Korridor\LaravelHasManySync\Tests\TestEnvironment\Models\Task;
1110
use Korridor\LaravelHasManySync\Tests\TestEnvironment\Models\User;
1211

@@ -272,7 +271,7 @@ public function testUpdateOnIdThatDoesNotBelongToRelationIgnoresTheEntryWithProb
272271
'content' => 'Updated Task 3 of Tester 2',
273272
]
274273
// Delete, because task with id=1 is missing
275-
], true, false);
274+
], throwOnIdNotInScope: false);
276275
} catch (\Throwable $throwable) {
277276
// Assert
278277
$this->fail();

0 commit comments

Comments
 (0)