Example analysis of soft deletion operation of laravel 5.1 framework model

Time:2021-10-17

This paper describes the soft deletion operation of larave5.1 framework model. Share with you for your reference, as follows:

Soft deletion is a more practical means of deletion. For example, you have an account and a record. You think it is wrong to delete it. After a few days, you find that it should not be deleted. At this time, the purpose of soft deletion is realized. You can find the deleted data for operation, either restore or real deletion.

1 General deletion

Before soft deletion, let’s take a look at the common deletion methods:

1.1 delete directly through primary key


  public function getDelete()
  {
    Article::destroy(1);
    Article::destroy([1,2,3]);
  }

1.2 delete after obtaining the model


  public function getDelete()
  {
    $article = Article::find(3);
    $article->delete();
  }

1.3 batch deletion

public function getDelete()
  {
    //Returns an integer that deletes several pieces of data
    $deleteRows = Article::where('id','>',3)->delete();
    dd($deleteRows);  // 2
  }

2 soft delete

2.1 preparation

If you want to implement soft deletion, you should do three things in advance:

  1. Add deleted_ At to the $date attribute of the model.
  2. Use the trait illuminate \ database \ eloquent \ softdeletes in the model
  3. Make sure you have deleted in your data table_ If there is no at column, add this column.

First, let’s do the first and second steps:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Article extends Model
{
  //Use the trait softdeletes
  use SoftDeletes;
  //White list
  protected $fillable = ['title', 'body'];
  // dates
  protected $dates = ['deleted_at'];
}

Then we generate a migration file to add deleted_ At column to data table:


class InsertDeleteAtIntroArticles extends Migration
{
  /**
   * Run the migrations.
   *
   * @return void
   */
  public function up()
  {
    Schema::table('articles', function (Blueprint $table) {
      $table->softDeletes();
    });
  }
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
    Schema::table('articles', function (Blueprint $table) {
      $table->dropSoftDeletes();
    });
  }
}

2.2 implementation of soft deletion

Now we can delete a piece of data and try:


  public function getDelete()
  {
    $article = Article::first();
    $article->delete();
  }

↑ when we delete this data, it means deleted in the data table_ At is not empty. It is a time value when delete_ When at is not empty, it proves that this data has been soft deleted.

2.3 judge whether the data is soft deleted

if ($article->trashed()){
      Echo 'this model has been soft deleted';
    }

2.4 query soft deleted data

It should be noted that when the data is soft deleted, it will be automatically excluded from the query data, that is, it cannot be queried by general query statements. When we want to query soft deleted data, we can use the withtraced method

public function getIndex()
  {
    $article = Article::withTrashed()->first();
    if ($article->trashed()){
      Echo 'soft deleted'// The code will execute to this line
    }
  }

We can also use only trashed. The difference between it and withtrashed is that it only obtains soft deleted data.


  public function getIndex()
  {
    $articles = Article::onlyTrashed()->where('id','<','10')->get()->toArray();
    dd($articles);
  }

2.5 recovering soft deleted data


  public function getIndex()
  {
    $article = Article::withTrashed()->find(6);
    $article->restore();
  }

2.6 permanently delete data


  public function getIndex()
  {
    $article = Article::withTrashed()->find(6);
    $article->forceDelete();
  }

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.