最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel 實(shí)現(xiàn)數(shù)據(jù)軟刪除功能

 更新時(shí)間:2019年08月21日 15:56:20   作者:寫(xiě)PHP的老王  
這篇文章主要介紹了Laravel 實(shí)現(xiàn)數(shù)據(jù)軟刪除功能,文中給大家提到了軟刪除功能的實(shí)現(xiàn)方法,需要的朋友可以參考下

對(duì)于任何一個(gè)模型,如果需要使用軟刪除功能,需要在模型中使用 Illuminate\Database\Eloquent\SoftDeletes 這個(gè)  trait 。軟刪除功能需要實(shí)現(xiàn)的功能有以下幾點(diǎn):

1.模型執(zhí)行刪除操作,只標(biāo)記刪除,不執(zhí)行真正的數(shù)據(jù)刪除

2.查詢的時(shí)候自動(dòng)過(guò)濾已經(jīng)標(biāo)記為刪除的數(shù)據(jù)

3.可以設(shè)置是否查詢已刪除的數(shù)據(jù),可以設(shè)置只查詢已刪除的數(shù)據(jù)

4.已刪除數(shù)據(jù)可以恢復(fù)

Model的軟刪除功能實(shí)現(xiàn)

Illuminate\Database\Eloquent\Model 中delete方法源碼:

public function delete()
{
 if (is_null($this->getKeyName())) {
  throw new Exception('No primary key defined on model.');
 }
 if (! $this->exists) {
  return;
 }
 if ($this->fireModelEvent('deleting') === false) {
  return false;
 }
 $this->touchOwners();
 $this->performDeleteOnModel();
 $this->fireModelEvent('deleted', false);
 return true;
}
protected function performDeleteOnModel()
{
 $this->setKeysForSaveQuery($this->newModelQuery())
 ->delete();
 $this->exists = false;
}

因?yàn)樵谧宇愔惺褂昧?nbsp;SoftDeletes trait,所以, SoftDeletes performDeleteOnModel 方法會(huì)覆蓋父類的方法,最終通過(guò)  runSoftDelete 方法更新刪除標(biāo)記。

protected function performDeleteOnModel()
{
 if ($this->forceDeleting) {
  $this->exists = false;
  return $this->newModelQuery()->where(
    $this->getKeyName(), $this->getKey()
  )->forceDelete();
 }
 return $this->runSoftDelete();
}

protected function runSoftDelete()
{
 $query = $this->newModelQuery()
      ->where($this->getKeyName(), $this->getKey());
 $time = $this->freshTimestamp();
 $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
 $this->{$this->getDeletedAtColumn()} = $time;
 if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
  $this->{$this->getUpdatedAtColumn()} = $time;
  $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
 }
 $query->update($columns);
}

Model查詢過(guò)濾刪除數(shù)據(jù)

Laravel中允許在Model中 static::addGlobalScope 方法添加全局的 Scope 。這樣就可以在查詢條件中添加一個(gè)全局條件。Laravel中軟刪除數(shù)據(jù)的過(guò)濾也是使用這種方式實(shí)現(xiàn)的。

SoftDeletes trait中加入了 Illuminate\Database\Eloquent\SoftDeletingScope 全局的 Scope 。并在 SoftDeletingScope 中實(shí)現(xiàn)查詢自動(dòng)過(guò)濾被刪除數(shù)據(jù),指定查詢已刪除數(shù)據(jù)功能。

public static function bootSoftDeletes()
{
 static::addGlobalScope(new SoftDeletingScope);
}

遠(yuǎn)程關(guān)聯(lián)數(shù)據(jù)的軟刪除處理

Scope的作用只在于當(dāng)前模型,以及關(guān)聯(lián)模型操作上。如果是遠(yuǎn)程關(guān)聯(lián),則還需要額外的處理。Laravel遠(yuǎn)程關(guān)聯(lián)關(guān)系通過(guò) hasManyThrough 實(shí)現(xiàn)。里面有兩個(gè)地方涉及到軟刪除的查詢。

protected function performJoin(Builder $query = null)
{
 $query = $query ?: $this->query;
 $farKey = $this->getQualifiedFarKeyName();
 $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $farKey);
 if ($this->throughParentSoftDeletes()) {
  $query->whereNull(
   $this->throughParent->getQualifiedDeletedAtColumn()
  );
 }
}

public function throughParentSoftDeletes()
{
 return in_array(SoftDeletes::class, class_uses_recursive(
  get_class($this->throughParent)
 ));
}
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
{
 $query->from( $query->getModel()->getTable().' as '
  .$hash = $this->getRelationCountHash()
 );
 $query->join($this->throughParent->getTable(), 
  $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondLocalKey
 );
 if ($this->throughParentSoftDeletes()) {
  $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
 }
 $query->getModel()->setTable($hash);
 return $query->select($columns)->whereColumn(
  $parentQuery->getQuery()->from.'.'.$query->getModel()->getKeyName(), '=', $this->getQualifiedFirstKeyName()
 );
}

performJoin 中通過(guò)中間模型關(guān)聯(lián)遠(yuǎn)程模型,會(huì)根據(jù) throughParentSoftDeletes 判斷中間模型是否有軟刪除,如果有軟刪除會(huì)過(guò)濾掉中間模型被刪除的數(shù)據(jù)。

以上就是Laravel實(shí)現(xiàn)軟刪除的大概邏輯。這里有一個(gè)細(xì)節(jié),Laravel中軟刪除的標(biāo)記是一個(gè)時(shí)間格式的字段,默認(rèn) delete_at 。通過(guò)是否為null判斷數(shù)據(jù)是否刪除。

但是有的時(shí)候,項(xiàng)目中會(huì)使用一個(gè)整形的字段標(biāo)記數(shù)據(jù)是否刪除。在這樣的場(chǎng)景下,需要對(duì)Laravel的軟刪除進(jìn)行修改才能夠?qū)崿F(xiàn)。

主要的方案是:

1.自定義 SoftDeletes trait,修改字段名稱,修改更新刪除標(biāo)記操作;

2.自定義 SoftDeletingScope 修改查詢條件

3.自定義 HasRelationships trait,在自定義的 HasRelationships 中重寫(xiě) newHasManyThrough 方法,實(shí)例化自定義的 HasManyThrough 對(duì)象

總結(jié)

以上所述是小編給大家介紹的Laravel 實(shí)現(xiàn)數(shù)據(jù)軟刪除功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • thinkPHP下ueditor的使用方法詳解

    thinkPHP下ueditor的使用方法詳解

    這篇文章主要介紹了thinkPHP下ueditor的使用方法,結(jié)合實(shí)例形式較為詳細(xì)的講述了thinkPHP下使用ueditor的具體步驟與相關(guān)技巧,需要的朋友可以參考下
    2015-12-12
  • PHP Hash算法:Times33算法代碼實(shí)例

    PHP Hash算法:Times33算法代碼實(shí)例

    這篇文章主要介紹了PHP Hash算法:Times33算法代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-05-05
  • 在Ubuntu 18.04上安裝PHP 7.3 7.2和7.0的方法

    在Ubuntu 18.04上安裝PHP 7.3 7.2和7.0的方法

    這篇文章主要介紹了在Ubuntu 18.04上安裝PHP 7.3 7.2和7.0的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • php中實(shí)現(xiàn)記住密碼下次自動(dòng)登錄的例子

    php中實(shí)現(xiàn)記住密碼下次自動(dòng)登錄的例子

    這篇文章主要介紹了php中實(shí)現(xiàn)記住密碼下次自動(dòng)登錄的例子,本文使用cookie實(shí)現(xiàn)記住密碼和自動(dòng)登錄功能,需要的朋友可以參考下
    2014-11-11
  • 淺析echo(),print(),print_r(),return之間的區(qū)別

    淺析echo(),print(),print_r(),return之間的區(qū)別

    這篇文章主要是對(duì)echo(),print(),print_r(),return之間的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-11-11
  • Thinkphp 框架擴(kuò)展之標(biāo)簽庫(kù)驅(qū)動(dòng)原理與用法分析

    Thinkphp 框架擴(kuò)展之標(biāo)簽庫(kù)驅(qū)動(dòng)原理與用法分析

    這篇文章主要介紹了Thinkphp 框架擴(kuò)展之標(biāo)簽庫(kù)驅(qū)動(dòng),結(jié)合實(shí)例形式分析了Thinkphp標(biāo)簽庫(kù)驅(qū)動(dòng)擴(kuò)展相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • 調(diào)試WordPress中定時(shí)任務(wù)的相關(guān)PHP腳本示例

    調(diào)試WordPress中定時(shí)任務(wù)的相關(guān)PHP腳本示例

    這篇文章主要介紹了調(diào)試WordPress中定時(shí)任務(wù)的相關(guān)PHP腳本示例,針對(duì)使用Cron API及wp_schedule_event()函數(shù)來(lái)寫(xiě)的定時(shí)任務(wù),需要的朋友可以參考下
    2015-12-12
  • php ZipArchive壓縮函數(shù)詳解實(shí)例

    php ZipArchive壓縮函數(shù)詳解實(shí)例

    在php中生成zip文件我們只要使用一個(gè)php zip壓縮ZipArchive函數(shù)就可以了,下面小編來(lái)給大家總結(jié)兩個(gè)實(shí)現(xiàn)一個(gè)是利用ZipArchive生成zip,另一個(gè)壓縮文件夾下所有文件
    2013-11-11
  • smarty簡(jiǎn)單應(yīng)用實(shí)例

    smarty簡(jiǎn)單應(yīng)用實(shí)例

    這篇文章主要介紹了smarty簡(jiǎn)單應(yīng)用實(shí)例,簡(jiǎn)單介紹了smarty中數(shù)據(jù)庫(kù)查詢、數(shù)組遍歷及調(diào)用模板顯示數(shù)據(jù)的相關(guān)技巧,簡(jiǎn)單易懂,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • thinkphp諸多限制條件下如何getshell詳解

    thinkphp諸多限制條件下如何getshell詳解

    這篇文章主要給大家介紹了關(guān)于thinkphp諸多限制條件下如何getshell的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論

襄樊市| 鄯善县| 满城县| 清镇市| 肃南| 宝丰县| 麟游县| 哈巴河县| 休宁县| 平阴县| 兴国县| 即墨市| 浦江县| 湖北省| 五大连池市| 吉木乃县| 任丘市| 札达县| 牟定县| 枞阳县| 霍林郭勒市| 石家庄市| 安康市| 吉木萨尔县| 宕昌县| 天台县| 福贡县| 宁乡县| 望城县| 西青区| 焦作市| 南昌市| 喀什市| 太湖县| 叙永县| 广德县| 姚安县| 固阳县| 剑阁县| 杭锦后旗| 揭东县|