Skip to content

lybc/laravel-better-softdelete

Repository files navigation

Laravel Better Soft Delete

Build Status StyleCI build status

Installing

$ composer require lybc/laravel-better-softdelete -vvv

Usage

在migrate中添加数据库结构

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

use Lybc\BetterSoftDelete\BetterSoftDeletes;

class SomeModel extends Model
{
    use BetterSoftDeletes;
}

完成以上操作即可使用 Laravel 提供的 API 进行软删除

example

如果没有使用本包提供的方式定义软删除字段,可以在模型中定义常量重写软删除字段

class SomeModel extends Model
{
    const DELETED_AT_COLUMN = 'deleted';
}

级联删除

本包提供级联删除支持,当模型之间存在关联关系时,父模型删除连带删除子模型

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

michaeldyrynda/laravel-cascade-soft-deletes

http://blog.dreamlikes.cn/archives/892

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages