在laravel中,軟刪除指的是數(shù)據(jù)表記錄并未真的從數(shù)據(jù)庫中刪除,而是將表記錄的表示狀態(tài)標(biāo)記為軟刪除,這樣在查詢時(shí)就可以進(jìn)行過濾,讓對應(yīng)表記錄看上去像是被“刪除”了。
本文操作環(huán)境:Windows10系統(tǒng)、Laravel6版、Dell G3電腦。
laravel中軟刪除的原理是什么
1、刪除模型
1.1 使用delete刪除模型
刪除模型很簡單,先獲取要?jiǎng)h除的模型實(shí)例,然后調(diào)用delete
方法即可:
$post = Post::find(5); if($post->delete()){ echo '刪除文章成功!'; }else{ echo '刪除文章失??!'; }
該方法返回true
或false
。
1.2 使用destroy刪除模型
當(dāng)然如果已知要?jiǎng)h除的模型id的話,可以用更簡單的方法destroy
直接刪除:
$deleted = Post::destroy(5);
你也可以一次傳入多個(gè)模型id刪除多個(gè)模型:
$deleted = Post::destroy([1,2,3,4,5]);
調(diào)用destroy
方法返回被刪除的記錄數(shù)。
1.3 使用查詢構(gòu)建器刪除模型
既然前面提到Eloquent模型本身就是查詢構(gòu)建器,也可以使用查詢構(gòu)建器風(fēng)格刪除模型,比如我們要?jiǎng)h除所有瀏覽數(shù)為0的文章,可以使用如下方式:
$deleted = ModelsPost::where('views', 0)->delete();
返回結(jié)果為被刪除的文章數(shù)。
2、軟刪除及其相關(guān)實(shí)現(xiàn)
2.1 軟刪除實(shí)現(xiàn)
上述刪除方法都會將數(shù)據(jù)表記錄從數(shù)據(jù)庫刪除,此外Eloquent模型還支持軟刪除。
所謂軟刪除指的是數(shù)據(jù)表記錄并未真的從數(shù)據(jù)庫刪除,而是將表記錄的標(biāo)識狀態(tài)標(biāo)記為軟刪除,這樣在查詢的時(shí)候就可以加以過濾,讓對應(yīng)表記錄看上去是被”刪除“了。Laravel中使用了一個(gè)日期字段作為標(biāo)識狀態(tài),這個(gè)日期字段可以自定義,這里我們使用deleted_at
,如果對應(yīng)模型被軟刪除,則deleted_at
字段的值為刪除時(shí)間,否則該值為空。
要讓Eloquent模型支持軟刪除,還要做一些設(shè)置。首先在模型類中要使用SoftDeletes
trait,該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 = 'posts'; //設(shè)置主鍵 public $primaryKey = 'id'; //設(shè)置日期時(shí)間格式 public $dateFormat = 'U'; protected $guarded = ['id','views','user_id','updated_at','created_at']; protected $dates = ['delete_at']; }
然后對應(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('posts', function (Blueprint $table) { $table->softDeletes(); }); } ...//其它方法 }
然后運(yùn)行:
php artisan migrate
這樣posts
中就有了deleted_at
列。接下來,我們在控制器中編寫測試代碼:
$post = Post::find(6); $post->delete(); if($post->trashed()){ echo '軟刪除成功!'; dd($post); }else{ echo '軟刪除失??!'; }
在瀏覽器中訪問http://laravel.app:8000/test
,頁面輸出如下:
當(dāng)我們再次通過下面這段代碼獲取所有文章:
$posts = Post::all(); dd($posts);
已經(jīng)看不到id為6的文章的身影了。
2.2 查詢結(jié)果包含軟刪除模型
那如果想要在查詢結(jié)果中包含軟刪除的記錄呢?可以使用SoftDeletes
trait上的withTrashed
方法:
$posts = Post::withTrashed()->get(); dd($posts);
執(zhí)行之后頁面顯示如下:
id為6的文章又出現(xiàn)在了查詢結(jié)果中。有時(shí)候我們只想要查看被軟刪除的模型,這也有招,通過SoftDeletes
上的onlyTrashed
方法即可:
$posts = Post::onlyTrashed()->get(); dd($posts);
執(zhí)行后頁面顯示結(jié)果如下:
2.3 軟刪除恢復(fù)
有時(shí)候我們需要恢復(fù)被軟刪除的模型,可以使用SoftDeletes
提供的restore
方法:
恢復(fù)單個(gè)模型
$post = Post::find(6); $post->restore();
恢復(fù)多個(gè)模型
Post::withTrashed()->where('id','>',1)->restore();
恢復(fù)所有模型
Post::withTrashed()->restore();
恢復(fù)關(guān)聯(lián)查詢模型
$post = Post::find(6); $post->history()->restore();
2.4 強(qiáng)制刪除
如果模型配置了軟刪除但我們確實(shí)要?jiǎng)h除改模型對應(yīng)數(shù)據(jù)庫表記錄,則可以使用SoftDeletes
提供的forceDelete
方法:
$post = Post::find(6); $post->forceDelete();
查看數(shù)據(jù)表可以發(fā)現(xiàn)id=6的表記錄已經(jīng)被刪除,不復(fù)存在:
【