Skip to content

Commit

Permalink
Add the SoftDeletes trait
Browse files Browse the repository at this point in the history
  • Loading branch information
forxer committed Apr 30, 2019
1 parent e3ab0e0 commit 753a24c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

2.2.0 (2019-04-30)
------------------

- Add the SoftDeletes trait

2.1.2 (2019-03-20)
------------------

Expand Down
29 changes: 29 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Includes some extensions/improvements to the Database section of Laravel Framewo
- [Default model sort](#default-model-sort)
- [Joins using relationships](#joins-using-relationships)
- [Query builder whereLike macro](#query-builder-wherelike-macro)
* [Soft deletes](#soft-deletes)

Installation
------------
Expand Down Expand Up @@ -198,3 +199,31 @@ By that:
```php
Post::whereLike(['name', 'text', 'author.name', 'tags.name'], $searchTerm)->get();
```

Soft deletes
------------

Our Soft Deletes trait extends the Eloquent one.

This allows us to provide the `scopeWithoutTrashedExcept` method :

```php
$postTypes = PostType::withoutTrashedExcept($post->post_type_id)->get();
```

To use it, add the trait `Axn\Illuminate\Database\Eloquent\SoftDeletes` to models:

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Axn\Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
use SoftDeletes;

// ...
}
```

17 changes: 17 additions & 0 deletions src/Eloquent/SoftDeletes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Axn\Illuminate\Database\Eloquent;

use Illuminate\Database\Eloquent\SoftDeletes as EloquentSoftDeletes;

trait SoftDeletes
{
use EloquentSoftDeletes;

public function scopeWithoutTrashedExcept($query, $exceptId)
{
return $query
->withoutTrashed()
->orWhere($this->getKeyName(), $exceptId);
}
}

0 comments on commit 753a24c

Please sign in to comment.