Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dillingham committed Oct 19, 2021
1 parent 89f96d5 commit a227fc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,27 @@ And add the trait and parent model to your child model:

namespace App\Models;

use App\Models\Author;
use Dillingham\SoftDeletesParent\SoftDeletesParent;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use SoftDeletesParent;

protected static $softDeletesParent = Author::class;
}
```
```php
<?php

namespace App\Providers;

class AppServiceProvider
{
public function register()
{
Post::softDeletesParent(Author::class);
}
}
```

### Scopes

Expand Down
8 changes: 6 additions & 2 deletions src/SoftDeletesParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;

trait SoftDeletesParent
{
protected static function bootSoftDeletesParent()
{
static::addGlobalScope(new SoftDeletesParentScope());
}

static::$dispatcher->listen('eloquent.deleting: '. static::$softDeletesParent, function (Model $model) {
public static function softDeletesParent($parent)
{
Event::listen('eloquent.deleting: '. $parent, function (Model $model) {
static::query()->where([
$model->getForeignKey() => $model->getKey(),
])->update(['parent_deleted_at' => now()]);
});

static::$dispatcher->listen('eloquent.restoring: '. static::$softDeletesParent, function ($model) {
Event::listen('eloquent.restoring: '. $parent, function (Model $model) {
static::query()->withParentTrashed()->where([
$model->getForeignKey() => $model->getKey(),
])->update(['parent_deleted_at' => null]);
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Dillingham\SoftDeletesParent\Tests;

use Dillingham\SoftDeletesParent\SoftDeletesParentServiceProvider;
use Dillingham\SoftDeletesParent\Tests\Fixtures\Author;
use Dillingham\SoftDeletesParent\Tests\Fixtures\Post;
use Illuminate\Database\Eloquent\Model;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -19,6 +21,8 @@ public function getEnvironmentSetUp($app)
{
Model::unguard();

Post::softDeletesParent(Author::class);

config()->set('database.default', 'testing');

$migration = include __DIR__ . '/Fixtures/Database/create_authors_table.php.stub';
Expand Down

0 comments on commit a227fc9

Please sign in to comment.