Giter VIP home page Giter VIP logo

laravel-softdeletes's Introduction

LARAVEL SOFTDELETES

StyleCI

Laravel softdeletes是一个用于替代Laravel内置的软删除softDelets功能的扩展包,可以把软删数据存储到独立的数据表中,从而不影响给原始表字段增加唯一索引,且可以起到提升性能的作用。

环境

  • PHP >= 7
  • laravel >= 5.5

安装

composer require dcat/laravel-softdeletes

简介

Laravel内置的softDelets功能会把软删的数据和正常数据存在同一个数据表中,这样会导致数据表的字段无法直接添加unique索引,因为很容易产生冲突,这样会给实际业务开发带来诸多不必要的困扰。 而Laravel softdeletes则可以把软删数据存储到独立的数据表中,从而不影响给原始表字段增加唯一索引,且可以起到提升性能的作用。

使用

首先需要创建两张字段一模一样的数据表,并且两张表都需要添加deleted_at字段

// 文章表
Schema::create('posts', function (Blueprint $table) {
	$table->bigIncrements('id');
	$table->string('title')->nullable();
	$table->string('body')->nullable();

	// 两张表都需要删除时间字段
	$table->timestamp('deleted_at')->nullable();

	$table->timestamps();
});

// 文章软删除表
Schema::create('posts_trash', function (Blueprint $table) {
	$table->bigIncrements('id');
	$table->string('title')->nullable();
	$table->string('body')->nullable();

	// 两张表都需要删除时间字段
	$table->timestamp('deleted_at')->nullable();

	$table->timestamps();
});

模型定义如下,默认的软删除表表名为:{原始表}_trash,如上面的posts表的默认软删除表表名是posts_trash

<?php

namespace App\Models;

use Dcat\Laravel\Database\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use SoftDeletes;
    
    /**
     * 自定义软删除表表名,默认为 {$table}_trash 
     * 
     * @var string 
     */
    protected $trashedTable = 'posts_trash';
    
    /**
     * 自定义软删除表表名,如有需要可以重写此方法
     * 
     * @return mixed
     */
    public function getTrashedTable()
    {
        return $this->trashedTable;
    }
}

除了withTrashed只能用于查询数据之外,其他方法的使用与Laravel内置的软删除功能完全一致,下面是简单的用法示例

需要注意withTrashed只能用于查询数据,不能用于updatedelete!!!

查询正常表数据

$posts = Post::where(...)->get();

仅查询软删除表数据 (onlyTrashed)

$trashedPosts = Post::onlyTrashed()->where(...)->get();

同时查询正常和软删数据 (withTrashed),需要注意withTrashed只能用于查询数据,不能用于updatedelete!!!

Post::withTrashed()
    ->where(...)
    ->offset(5)
    ->limit(5)
    ->get();

// 可以使用子查询以及whereHas等
Post::withTrashed()
    ->whereHas('...', function ($q) {
        $q->where(...);
    })
    ->offset(5)
    ->limit(5)
    ->get();
    
// 分页
Post::withTrashed()
    ->whereHas('...', function ($q) {
 	$q->where(...);
    })
    ->paginate(10);

软删除/硬删除/还原

$post = Post::first();

// 软删除
$post->delete();

// 还原
$post->restore();

// 硬删
$post->forceDelete();

// 批量软删
Post::where(...)->delete();

// 批量硬删
Post::onlyTrashed()->where(...)->delete();

License

The MIT License (MIT).

laravel-softdeletes's People

Contributors

jqhph avatar

Stargazers

 avatar  avatar 听风吹雨 avatar 塵世不再 avatar  avatar  avatar  avatar xyxu avatar  avatar Eveleen avatar 星尘 avatar alai13 avatar test avatar Qiaodows avatar jorry有桥 avatar Terran avatar 铮gg avatar imnpc avatar  avatar Kevin avatar  avatar 奔跑 avatar Nguyen Gia Hao avatar  avatar wilbur.yu avatar Talent Su avatar baijunyao avatar gubin avatar guanguans avatar sangshu avatar  avatar BuWenGuiQi_ avatar 牛铁柱 avatar  avatar  avatar Lenix avatar

Watchers

 avatar  avatar

laravel-softdeletes's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.