-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete attachments when model is deleted (fixes #445)
- Loading branch information
1 parent
9c0516d
commit 0be2873
Showing
9 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Azuriom\Models\Traits; | ||
|
||
use Illuminate\Contracts\Database\Query\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* Parent class for models that can have attachments. | ||
* Used to automatically delete attachments from the associated children. | ||
*/ | ||
trait AttachableParent | ||
{ | ||
public static function bootAttachableParent(): void | ||
{ | ||
$pendingDelete = []; | ||
|
||
static::deleting(function (Model $model) use (&$pendingDelete) { | ||
$children = $model->getAttribute(static::$attachableRelation ?? null); | ||
|
||
if ($children !== null) { | ||
// The relation can be either a collection or a single model | ||
$pendingDelete[$model->getKey()] = collect($children->load([ | ||
'attachments' => fn (Builder $query) => $query->withTrashed(), | ||
])); | ||
} | ||
}); | ||
|
||
static::deleted(function (Model $model) use (&$pendingDelete) { | ||
$children = Arr::get($pendingDelete, $model->getKey(), []); | ||
|
||
foreach ($children as $child) { | ||
if ($child->fresh() !== null) { | ||
continue; // Ensure the model is actually deleted | ||
} | ||
|
||
foreach ($child->attachments as $attachment) { | ||
$attachment->setRelation('attachable', $child)->forceDelete(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters