Laravel5.1 框架模型軟刪除操作實(shí)例分析
本文實(shí)例講述了Laravel5.1 框架模型軟刪除操作。分享給大家供大家參考,具體如下:
軟刪除是比較實(shí)用的一種刪除手段,比如說 你有一本賬 有一筆記錄你覺得不對給刪了 過了幾天發(fā)現(xiàn)不應(yīng)該刪除,這時(shí)候軟刪除的目的就實(shí)現(xiàn)了 你可以找到已經(jīng)被刪除的數(shù)據(jù)進(jì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() { // 返回一個(gè)整形 刪除了幾條數(shù)據(jù) $deleteRows = Article::where('id','>',3)->delete(); dd($deleteRows); // 2 }
2 軟刪除
2.1 準(zhǔn)備工作
如果你要實(shí)現(xiàn)軟刪除 你應(yīng)該提前做3件事情:
- 添加deleted_at 到模型的 $date 屬性中。
- 在模型中使用 Illuminate\Database\Eloquent\SoftDeletes 這個(gè)trait
- 保證你的數(shù)據(jù)表中有deleted_at列 如果沒有就添加這個(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è)遷移文件來增加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ù)被軟刪除后 它會自動(dòng)從查詢數(shù)據(jù)中排除、就是它無法被一般的查詢語句查詢到。當(dāng)我們想要查詢軟刪除數(shù)據(jù)時(shí) 可以使用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 恢復(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)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
上一篇:Laravel5.1 框架表單驗(yàn)證操作實(shí)例詳解
欄 目:PHP編程
下一篇:PHP字符串與數(shù)組處理函數(shù)用法小結(jié)
本文標(biāo)題:Laravel5.1 框架模型軟刪除操作實(shí)例分析
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/10979.html
您可能感興趣的文章
- 01-11thinkphp框架類庫擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問題的分析與解決
- 01-11php 實(shí)現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel框架基礎(chǔ)語法與知識點(diǎn)整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實(shí)現(xiàn)方法分析
- 01-11laravel5.5框架的上傳圖片功能實(shí)例分析【僅傳到服務(wù)器端】


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地?cái)?shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計(jì)方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子