主頁(yè) > 知識(shí)庫(kù) > Laravel5.1 框架模型軟刪除操作實(shí)例分析

Laravel5.1 框架模型軟刪除操作實(shí)例分析

熱門標(biāo)簽:銀行業(yè)務(wù) 阿里云 服務(wù)器配置 電子圍欄 團(tuán)購(gòu)網(wǎng)站 Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Linux服務(wù)器

本文實(shí)例講述了Laravel5.1 框架模型軟刪除操作。分享給大家供大家參考,具體如下:

軟刪除是比較實(shí)用的一種刪除手段,比如說(shuō) 你有一本賬 有一筆記錄你覺(jué)得不對(duì)給刪了 過(guò)了幾天發(fā)現(xiàn)不應(yīng)該刪除,這時(shí)候軟刪除的目的就實(shí)現(xiàn)了 你可以找到已經(jīng)被刪除的數(shù)據(jù)進(jìn)行操作 可以是還原也可以是真正的刪除。

1 普通刪除

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

1.1 直接通過(guò)主鍵刪除

  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()
  {
    // 返回一個(gè)整形 刪除了幾條數(shù)據(jù)
    $deleteRows = Article::where('id','>',3)->delete();
    dd($deleteRows);  // 2
  }

2 軟刪除

2.1 準(zhǔn)備工作

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

  1. 添加deleted_at 到模型的 $date 屬性中。
  2. 在模型中使用 Illuminate\Database\Eloquent\SoftDeletes 這個(gè)trait
  3. 保證你的數(shù)據(jù)表中有deleted_at列 如果沒(méi)有就添加這個(gè)列。

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

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

然后我們生成一個(gè)遷移文件來(lái)增加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 實(shí)現(xiàn)軟刪除

現(xiàn)在我們就可以刪除一條數(shù)據(jù)試試?yán)玻?/p>

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

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

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

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

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

有一點(diǎn)需要注意,當(dāng)數(shù)據(jù)被軟刪除后 它會(huì)自動(dòng)從查詢數(shù)據(jù)中排除、就是它無(wú)法被一般的查詢語(yǔ)句查詢到。當(dāng)我們想要查詢軟刪除數(shù)據(jù)時(shí) 可以使用withTrashed方法

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

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

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

2.5 恢復(fù)被軟刪除的數(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();
  }

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • 在laravel中實(shí)現(xiàn)ORM模型使用第二個(gè)數(shù)據(jù)庫(kù)設(shè)置
  • 使用laravel的Eloquent模型如何獲取數(shù)據(jù)庫(kù)的指定列
  • Laravel5.1 框架關(guān)聯(lián)模型之后操作實(shí)例分析
  • Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法實(shí)例分析
  • Laravel5.1 框架模型遠(yuǎn)層一對(duì)多關(guān)系實(shí)例分析
  • Laravel5.1 框架模型一對(duì)一關(guān)系實(shí)現(xiàn)與使用方法實(shí)例分析
  • Laravel5.1 框架模型查詢作用域定義與用法實(shí)例分析
  • Laravel5.1 框架模型創(chuàng)建與使用方法實(shí)例分析
  • Laravel框架視圖和模型操作方法分析
  • Laravel 5框架學(xué)習(xí)之模型、控制器、視圖基礎(chǔ)流程
  • laravel學(xué)習(xí)教程之關(guān)聯(lián)模型
  • laravel框架模型和數(shù)據(jù)庫(kù)基礎(chǔ)操作實(shí)例詳解

標(biāo)簽:蚌埠 棗莊 衢州 大理 江蘇 廣元 萍鄉(xiāng) 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel5.1 框架模型軟刪除操作實(shí)例分析》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266