对于任何一个模型,如果需要使用软删除功能,需要在模型中使用 illuminate\database\eloquent\softdeletes 这个  trait 。软删除功能需要实现的功能有以下几点:

1.模型执行删除操作,只标记删除,不执行真正的数据删除

2.查询的时候自动过滤已经标记为删除的数据

3.可以设置是否查询已删除的数据,可以设置只查询已删除的数据

4.已删除数据可以恢复

model的软删除功能实现

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;
}

因为在子类中使用了 softdeletes trait,所以, softdeletes performdeleteonmodel 方法会覆盖父类的方法,最终通过  runsoftdelete 方法更新删除标记。

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查询过滤删除数据

laravel中允许在model中 static::addglobalscope 方法添加全局的 scope 。这样就可以在查询条件中添加一个全局条件。laravel中软删除数据的过滤也是使用这种方式实现的。

softdeletes trait中加入了 illuminate\database\eloquent\softdeletingscope 全局的 scope 。并在 softdeletingscope 中实现查询自动过滤被删除数据,指定查询已删除数据功能。

public static function bootsoftdeletes()
{
 static::addglobalscope(new softdeletingscope);
}

远程关联数据的软删除处理

scope的作用只在于当前模型,以及关联模型操作上。如果是远程关联,则还需要额外的处理。laravel远程关联关系通过 hasmanythrough 实现。里面有两个地方涉及到软删除的查询。

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 中通过中间模型关联远程模型,会根据 throughparentsoftdeletes 判断中间模型是否有软删除,如果有软删除会过滤掉中间模型被删除的数据。

以上就是laravel实现软删除的大概逻辑。这里有一个细节,laravel中软删除的标记是一个时间格式的字段,默认 delete_at 。通过是否为null判断数据是否删除。

但是有的时候,项目中会使用一个整形的字段标记数据是否删除。在这样的场景下,需要对laravel的软删除进行修改才能够实现。

主要的方案是:

1.自定义 softdeletes trait,修改字段名称,修改更新删除标记操作;

2.自定义 softdeletingscope 修改查询条件

3.自定义 hasrelationships trait,在自定义的 hasrelationships 中重写 newhasmanythrough 方法,实例化自定义的 hasmanythrough 对象

总结

以上所述是www.887551.com给大家介绍的laravel 实现数据软删除功能,希望对大家有所帮助