Skip to content

Commit 32fddc1

Browse files
author
Piotr Krajewski
committed
add destroy attribute
1 parent 25ebe96 commit 32fddc1

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ usage:
103103
]);
104104
```
105105

106+
to delete nested row you should pass `_destroy` attribute:
107+
108+
````
109+
\App\Post::findOrFail(1)->update([
110+
'title' => 'Better text',
111+
112+
'option' => [
113+
'info' => 'better info'
114+
],
115+
116+
'comments' => [
117+
[
118+
'id' => 2,
119+
'_destroy' => true
120+
],
121+
]
122+
]);
123+
```
124+
125+
106126
## Change log
107127
108128
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"keywords": [
66
"Laravel", "Lumen", "Eloquent", "Nested Attributes"
77
],
8-
"version": "0.0.4",
8+
"version": "0.0.5",
99
"homepage": "https://github.com/mits87/eloquent-nested-attributes",
1010
"license": "MIT",
1111
"authors": [

src/Traits/HasNestedAttributesTrait.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ trait HasNestedAttributesTrait
1919
*/
2020
protected $acceptNestedAttributesFor = [];
2121

22+
/**
23+
* Defined "destroy" key name
24+
*
25+
* @var string
26+
*/
27+
protected $destroyNestedKey = '_destroy';
28+
29+
2230
/**
2331
* Get accept nested attributes
2432
*
@@ -80,7 +88,7 @@ public function save(array $options = [])
8088
}
8189
} else if ($relation instanceof HasMany || $relation instanceof MorphMany) {
8290
foreach ($stack as $params) {
83-
if (!$this->saveManyNestedAttributes($relation, $params)) {
91+
if (!$this->saveManyNestedAttributes($this->$methodName(), $params)) {
8492
return false;
8593
}
8694
}
@@ -103,6 +111,9 @@ public function save(array $options = [])
103111
protected function saveNestedAttributes($relation, array $params)
104112
{
105113
if ($this->exists && $model = $relation->first()) {
114+
if ($this->allowDestroyNestedAttributes($params)) {
115+
return $model->delete();
116+
}
106117
return $model->update($stack);
107118
} else if ($relation->create($stack)) {
108119
return true;
@@ -121,10 +132,25 @@ protected function saveManyNestedAttributes($relation, array $params)
121132
{
122133
if (isset($params['id']) && $this->exists) {
123134
$model = $relation->findOrFail($params['id']);
135+
136+
if ($this->allowDestroyNestedAttributes($params)) {
137+
return $model->delete();
138+
}
124139
return $model->update($params);
125140
} else if ($relation->create($params)) {
126141
return true;
127142
}
128143
return false;
129144
}
145+
146+
/**
147+
* Check can we delete nested data
148+
*
149+
* @param array $params
150+
* @return bool
151+
*/
152+
protected function allowDestroyNestedAttributes(array $params)
153+
{
154+
return isset($params[$this->destroyNestedKey]) && (bool) $params[$this->destroyNestedKey] == true;
155+
}
130156
}

0 commit comments

Comments
 (0)