溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Laravel5.1 框架怎么實現(xiàn)模型軟刪除操作

發(fā)布時間:2021-04-13 15:53:33 來源:億速云 閱讀:184 作者:Leah 欄目:開發(fā)技術

使用Laravel5.1 框架怎么實現(xiàn)模型軟刪除操作?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1 普通刪除

在軟刪除之前咱先看看普通的刪除方法:

1.1 直接通過主鍵刪除

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

1.2 獲取model后刪除

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

1.3 批量刪除

  public function getDelete()
  {
    // 返回一個整形 刪除了幾條數(shù)據(jù)
    $deleteRows = Article::where('id','>',3)->delete();
    dd($deleteRows);  // 2
  }

2 軟刪除

2.1 準備工作

如果你要實現(xiàn)軟刪除 你應該提前做3件事情:

  1. 添加deleted_at 到模型的 $date 屬性中。

  2. 在模型中使用 Illuminate\Database\Eloquent\SoftDeletes 這個trait

  3. 保證你的數(shù)據(jù)表中有deleted_at列 如果沒有就添加這個列。

首先我們做第一步和第二步:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Article extends Model
{
  // 使用SoftDeletes這個trait
  use SoftDeletes;
  // 白名單
  protected $fillable = ['title', 'body'];
  // dates
  protected $dates = ['deleted_at'];
}

然后我們生成一個遷移文件來增加deleted_at列到數(shù)據(jù)表:

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 實現(xiàn)軟刪除

現(xiàn)在我們就可以刪除一條數(shù)據(jù)試試啦:

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

↑ 當我們刪了這條數(shù)據(jù)后 在數(shù)據(jù)表中的表示是 deleted_at 不為空 它是一個時間值,當delete_at不為空時 證明這條數(shù)據(jù)已經(jīng)被軟刪除了。

2.3 判斷數(shù)據(jù)是否被軟刪除

if ($article->trashed()){
      echo '這個模型已經(jīng)被軟刪除了';
    }

2.4 查詢到被軟刪除的數(shù)據(jù)

有一點需要注意,當數(shù)據(jù)被軟刪除后 它會自動從查詢數(shù)據(jù)中排除、就是它無法被一般的查詢語句查詢到。當我們想要查詢軟刪除數(shù)據(jù)時 可以使用withTrashed方法

  public function getIndex()
  {
    $article = Article::withTrashed()->first();
    if ($article->trashed()){
      echo '被軟刪除了';  // 代碼會執(zhí)行到這一行
    }
  }

我們還可以使用onlyTrashed,它和withTrashed的區(qū)別是 它只獲得軟刪除的數(shù)據(jù)。

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

2.5 恢復被軟刪除的數(shù)據(jù)

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

2.6 永久刪除數(shù)據(jù)

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

關于使用Laravel5.1 框架怎么實現(xiàn)模型軟刪除操作問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI