From 83dc31f6c5afe1b68207779c3054a53493c090e5 Mon Sep 17 00:00:00 2001 From: kolirt Date: Fri, 25 Oct 2024 19:26:40 +0300 Subject: [PATCH] add BelongsToMany saving --- README.md | 66 ++++++++++++++++++++++++++++++++++++++ composer.json | 2 +- src/Traits/MasterModel.php | 16 +++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f48b358..d8d80a6 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 diff --git a/composer.json b/composer.json index 9153126..46dd312 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/Traits/MasterModel.php b/src/Traits/MasterModel.php index 36da5eb..2720db9 100644 --- a/src/Traits/MasterModel.php +++ b/src/Traits/MasterModel.php @@ -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, @@ -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; } }