whereNull($model->getQualifiedDeletedAtColumn()); }else{ // $builder->where($model->getQualifiedDeletedAtColumn(),$model::STATUS_ENABLED); } } /** * Extend the query builder with the needed functions. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ public function extend(Builder $builder) { foreach ($this->extensions as $extension) { $this->{"add{$extension}"}($builder); } $builder->onDelete(function (Builder $builder) { $column = $this->getDeletedAtColumn($builder); $model = $builder->getModel(); return $builder->update([ $column => $model->getDeletedColumnValue(), ]); }); } /** * Add the restore extension to the builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addRestore(Builder $builder) { $builder->macro('restore', function (Builder $builder) { $builder->withAll(); $model = $builder->getModel(); return $builder->update([$model->getDeletedAtColumn() => $model::STATUS_ENABLED]); }); } /** * 筛选出正常状态和删除状态的数据 * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addWithTrashed(Builder $builder) { $builder->macro('withTrashed', function (Builder $builder) { $builder->withoutGlobalScope($this); $model = $builder->getModel(); return $builder->whereIn($model::DELETED_AT,[$model::STATUS_DELETED,$model::STATUS_ENABLED]); }); } /** * 只筛选出删除的数据 * * @param \Illuminate\Database\Eloquent\Builder $builder * @return void */ protected function addOnlyTrashed(Builder $builder) { $builder->macro('onlyTrashed', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->where( $model->getQualifiedDeletedAtColumn(),$model::STATUS_DELETED ); return $builder; }); } /** * 筛选出正常状态和禁用状态的数据 * @param Builder $builder */ protected function addWithDisabled(Builder $builder){ $builder->macro('withDisabled', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->whereIn( $model->getQualifiedDeletedAtColumn(),[$model::STATUS_DISABLED,$model::STATUS_ENABLED] ); return $builder; }); } /** * 只筛选出禁用状态的数据 * @param Builder $builder */ protected function addOnlyDisabled(Builder $builder){ $builder->macro('onlyDisabled', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->where( $model->getQualifiedDeletedAtColumn(),$model::STATUS_DISABLED ); return $builder; }); } /** * 筛选出所有数据 * @param Builder $builder */ protected function addWithAll(Builder $builder){ $builder->macro('withAll', function (Builder $builder) { return $builder->withoutGlobalScope($this); }); } }