一文了解laravel模型刪除和軟刪除

一文了解laravel模型刪除和軟刪除

1、刪除模型

1.1 使用delete刪除模型

刪除模型很簡單,先獲取要刪除的模型實(shí)例,然后調(diào)用delete方法即可:

$post?=?Post::find(5); if($post->delete()){ ????echo?'刪除文章成功!'; }else{ ????echo?'刪除文章失敗!'; }

該方法返回true或false。

1.2 使用destroy刪除模型

當(dāng)然如果已知要刪除的模型id的話,可以用更簡單的方法destroy直接刪除:

$deleted?=?Post::destroy(5);

你也可以一次傳入多個模型id刪除多個模型:

$deleted?=?Post::destroy([1,2,3,4,5]);

調(diào)用destroy方法返回被刪除的記錄數(shù)。

1.3 使用查詢構(gòu)建器刪除模型

既然前面提到Eloquent模型本身就是查詢構(gòu)建器,也可以使用查詢構(gòu)建器風(fēng)格刪除模型,比如我們要刪除所有瀏覽數(shù)為0的文章,可以使用如下方式:

$deleted?=?ModelsPost::where('views',?0)->delete();

返回結(jié)果為被刪除的文章數(shù)。

2、軟刪除實(shí)現(xiàn)

上述刪除方法都會將數(shù)據(jù)表記錄從數(shù)據(jù)庫刪除,此外Eloquent模型還支持軟刪除。

所謂軟刪除指的是數(shù)據(jù)表記錄并未真的從數(shù)據(jù)庫刪除,而是將表記錄的標(biāo)識狀態(tài)標(biāo)記為軟刪除,這樣在查詢的時候就可以加以過濾,讓對應(yīng)表記錄看上去是被”刪除“了。laravel中使用了一個日期字段作為標(biāo)識狀態(tài),這個日期字段可以自定義,這里我們使用deleted_at,如果對應(yīng)模型被軟刪除,則deleted_at字段的值為刪除時間,否則該值為空。

要讓Eloquent模型支持軟刪除,還要做一些設(shè)置。首先在模型類中要使用SoftDeletestrait,該trait為軟刪除提供一系列相關(guān)方法,具體可參考源碼IlluminateDatabaseEloquentSoftDeletes,此外還要設(shè)置$date屬性數(shù)組,將deleted_at置于其中:

<?php namespace AppModels; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes; class Post extends Model {     use SoftDeletes;     //設(shè)置表名     public $table = &#39;posts&#39;;     //設(shè)置主鍵     public $primaryKey = &#39;id&#39;;     //設(shè)置日期時間格式     public $dateFormat = &#39;U&#39;;     protected $guarded = [&#39;id&#39;,&#39;views&#39;,&#39;user_id&#39;,&#39;updated_at&#39;,&#39;created_at&#39;];     protected $dates = [&#39;delete_at&#39;]; }

然后對應(yīng)的數(shù)據(jù)庫posts中添加deleted_at列,我們使用遷移來實(shí)現(xiàn),先執(zhí)行Artisan命令:

php?artisan?make:migration?alter_posts_deleted_at?--table=posts

然后編輯生成的PHP文件如下:

<?php use IlluminateDatabaseSchemaBlueprint; use IlluminateDatabaseMigrationsMigration; class AlterPostsDeletedAt extends Migration {     /**      * Run the migrations.      *      * @return void      */     public function up()     {         Schema::table(&#39;posts&#39;, function (Blueprint $table) {             $table->softDeletes(); ????????}); ????} ????...//其它方法 }

然后運(yùn)行:

php?artisan?migrate

這樣posts中就有了deleted_at列。接下來,我們在控制器中編寫測試代碼:

$post?=?Post::find(6); $post-&gt;delete(); if($post-&gt;trashed()){ ????echo?'軟刪除成功!'; ????dd($post); }else{ ????echo?'軟刪除失敗!'; }

那如果想要在查詢結(jié)果中包含軟刪除的記錄呢?可以使用SoftDeletes trait上的withTrashed方法:

$posts?=?Post::withTrashed()-&gt;get(); dd($posts);

有時候我們只想要查看被軟刪除的模型,這也有招,通過SoftDeletes上的onlyTrashed方法即可:

$posts?=?Post::onlyTrashed()-&gt;get(); dd($posts);

軟刪除恢復(fù)

有時候我們需要恢復(fù)被軟刪除的模型,可以使用SoftDeletes提供的restore方法:

恢復(fù)單個模型

$post?=?Post::find(6); $post-&gt;restore();

有點(diǎn)問題,ID為6的已經(jīng)被軟刪除了,Post::find(6) 是查不到數(shù)據(jù)的,

應(yīng)該

$post?=?Post::withTrashed()-&gt;find(6); $post-&gt;restore();

恢復(fù)多個模型

Post::withTrashed()-&gt;where('id','&gt;',1)-&gt;restore();

恢復(fù)所有模型

Post::withTrashed()-&gt;restore();

恢復(fù)關(guān)聯(lián)查詢模型

$post?=?Post::find(6); $post-&gt;history()-&gt;restore();

強(qiáng)制刪除

如果模型配置了軟刪除但我們確實(shí)要刪除改模型對應(yīng)數(shù)據(jù)庫表記錄,則可以使用SoftDeletes提供的forceDelete方法:

$post?=?Post::find(6); $post-&gt;forceDelete();

PHP中文網(wǎng),大量的免費(fèi)laravel入門教程,歡迎在線學(xué)習(xí)!

本文轉(zhuǎn)自:https://blog.csdn.net/weixin_38112233/article/details/78574007

以上就是一文了解

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊8 分享