https://github.com/lybc/laravel-better-softdelete
https://github.com/lybc/laravel-better-softdelete
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lybc/laravel-better-softdelete
- Owner: lybc
- Created: 2018-12-07T08:24:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-11T03:10:59.000Z (over 7 years ago)
- Last Synced: 2024-04-19T16:09:08.224Z (about 2 years ago)
- Language: PHP
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Better Soft Delete
[](https://travis-ci.org/lybc/laravel-better-softdelete)


## Installing
```bash
$ composer require lybc/laravel-better-softdelete -vvv
```
## Usage
### 在migrate中添加数据库结构
```php
public function up()
{
Schema::create('some_tables', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
...
$table->betterSoftDeletes();
});
}
public funtion down()
{
Schema::table('some_tables', function (Blueprint $table) {
...
$table->dropBetterSoftDeletes();
});
}
```
### 在 model 中 use Trait
```php
use Lybc\BetterSoftDelete\BetterSoftDeletes;
class SomeModel extends Model
{
use BetterSoftDeletes;
}
```
完成以上操作即可使用 Laravel 提供的 API 进行软删除
[example](https://github.com/lybc/laravel-better-softdelete/blob/master/tests/DbSchemaTest.php)
如果没有使用本包提供的方式定义软删除字段,可以在模型中定义常量重写软删除字段
```php
class SomeModel extends Model
{
const DELETED_AT_COLUMN = 'deleted';
}
```
### 级联删除
本包提供级联删除支持,当模型之间存在关联关系时,父模型删除连带删除子模型
```php
use Lybc\BetterSoftDelete\BetterSoftDeletes;
class Post extends Model
{
use BetterSoftDeletes;
// 定义需要级联删除的关联关系
protected $cascadeDeletes = [
'comments'
];
public function comments()
{
return $this->hasMany(Comment::class);
}
}
```
## License
MIT
## thanks
[package-builder](https://github.com/overtrue/package-builder)
[michaeldyrynda/laravel-cascade-soft-deletes](https://github.com/michaeldyrynda/laravel-cascade-soft-deletes)
[http://blog.dreamlikes.cn/archives/892](http://blog.dreamlikes.cn/archives/892)