Skip to content

Commit

Permalink
add BelongsToMany saving
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Oct 25, 2024
1 parent 328a01e commit 83dc31f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ code complexity and enhancing performance.
- [Saving `HasOne`, `MorphOne` relations](#saving-hasone-morphone-relations)
- [Saving `HasMany`, `MorphMany` relations](#saving-hasmany-morphmany-relations)
- [Saving `HasMany`, `MorphMany` relations with `sync` mode](#saving-hasmany-morphmany-relations-with-sync-mode)
- [Saving `BelongsToMany` relation](#saving-belongstomany-relation)
- [Saving `BelongsToMany` relation with `sync` mode](#saving-belongstomany-relation-with-sync-mode)
- [Response file](#response-file)
- [FAQ](#faq)
- [License](#license)
- [Other packages](#other-packages)
Expand Down Expand Up @@ -167,6 +170,7 @@ $item->update([
]);
```


### Saving `HasMany`, `MorphMany` relations
You can **save** `HasMany`, `MorphMany` relations in the same way as a file. If relations exists, it will be updated, otherwise it will be created

Expand Down Expand Up @@ -210,6 +214,68 @@ $item->update([
```


### Saving `BelongsToMany` relation

```php
$item = Item::query()->first();

$item->update([
'categories' => [1, 2, 3] // belongsToMany relations
]);

$item->update([
'categories' => [ // belongsToMany relation
1 => ['name' => 'Category 1'],
2 => ['name' => 'Category 2'],
3 => ['name' => 'Category 3']
]
]);
```


### Saving `BelongsToMany` relation with `sync` mode
You can **sync** the `BelongsToMany` relation. Everything that is not specified when saving will be deleted

```php
$item = Item::query()->first();

$item->update([
'categories' => [ // belongsToMany relation
'mode' => 'sync', // not specified relations will be deleted
'value' => [1, 2, 3]
]
]);

$item->update([
'categories' => [ // belongsToMany relation
'mode' => 'sync', // not specified relations will be deleted
'value' => [
1 => ['name' => 'Category 1'],
2 => ['name' => 'Category 2'],
3 => ['name' => 'Category 3']
]
]
]);
```


### Response file
Use the **responseFile** method to return a file in a controller

```php
class FileController extends Controller
{

public function index()
{
$item = Item::query()->first();
return $item->responseFile('image');
}

}
```


## FAQ
Check closed [issues](https://github.com/kolirt/laravel-master-model/issues) to get answers for most asked questions

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"homepage": "https://github.com/kolirt/laravel-master-model",
"license": "MIT",
"version": "4.0.3",
"version": "4.0.4",
"authors": [
{
"name": "kolirt"
Expand Down
16 changes: 16 additions & 0 deletions src/Traits/MasterModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function fill(array $attributes)
case $relation instanceof \Illuminate\Database\Eloquent\Relations\MorphOne:
case $relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany:
case $relation instanceof \Illuminate\Database\Eloquent\Relations\MorphMany:
case $relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany:
$this->relations_to_save[] = [
'relation_name' => $key,
'relation' => $relation,
Expand Down Expand Up @@ -278,6 +279,21 @@ public function save(array $options = [])
$this->setRelation($relation_name, $loaded_relations);
}

break;

/**
* Save BelongsToMany relation
*/
case $relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany:
$mode = $value['mode'] ?? null;
$value = key_exists('value', $value) ? $value['value'] : $value;

if ($mode === 'sync') {
$relation->sync($value);
} else {
$relation->syncWithoutDetaching($value);
}

break;
}
}
Expand Down

0 comments on commit 83dc31f

Please sign in to comment.