Skip to content

Commit

Permalink
Merge pull request #10 from stfndamjanovic/main
Browse files Browse the repository at this point in the history
Remove flags on model delete
  • Loading branch information
freekmurze authored Nov 9, 2022
2 parents 75954fb + c51a7e6 commit b35056a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Models/Concerns/HasFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\ModelFlags\Models\Flag;
use Illuminate\Database\Eloquent\Model;

/** @mixin \Illuminate\Database\Eloquent\Model */
/** @mixin Model */
trait HasFlags
{
public static function bootHasFlags()
{
static::deleted(function (Model $deletedModel) {
$deletedModel->flags()->delete();
});
}

public function flags(): MorphMany
{
return $this->morphMany(config('model-flags.flag_model'), 'flaggable');
Expand Down
12 changes: 12 additions & 0 deletions tests/HasFlagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,15 @@
$this->otherModel->flag('flag-a');
expect(TestModel::notFlagged('flag-a')->get())->toHaveCount(0);
});

it('can_remove_flags_on_model_delete', function () {
$this->model->flag('flag-a');

$this->model->delete();

$this->assertEquals(0, $this->model->flags()->get()->count());
});

it('can_remove_model_without_flags', function () {
expect($this->model->delete())->toBeTrue();
});

0 comments on commit b35056a

Please sign in to comment.