数据软删除
<h1>数据软删除</h1>
<p>先参考<code>Laravel</code>文档实现模型的<a href="https://learnku.com/docs/laravel/6.x/eloquent/5176#soft-deleting">软删除</a>:</p>
<pre><code class="language-php">&lt;?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
}</code></pre>
<p>这样在<code>grid</code>列表中显示的数据都是未被删除的数据</p>
<pre><code class="language-php">return Grid::make(new Post(), function (Grid $grid) {
$grid-&gt;id('ID')-&gt;sortable();
$grid-&gt;title('Title');
$grid-&gt;created_at('Created at');
$grid-&gt;updated_at('Updated at');
});</code></pre>
<h2>回收站入口</h2>
<p>接下来需要增加一个入口,能让我们看到被软删除的数据,这里可以使用<code>model-grid</code>的<a href="model-grid-filters.md#scope">范围过滤器</a>来实现</p>
<pre><code class="language-php">$grid-&gt;filter(function () {
// 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。
$filter-&gt;scope('trashed', '回收站')-&gt;onlyTrashed();
});</code></pre>
<p>在表头的筛选按钮的下拉菜单中就会出现一个<code>回收站</code>按钮,点击它,就会调用模型的<code>onlyTrashed</code>方法,从表中查询出被删除的数据,也就是回收站中的数据。</p>
<p><img style="box-shadow:0 1px 6px 1px rgba(0, 0, 0, 0.12)" width="40%" src="{{public}}/assets/img/screenshots/trash-button.png"></p>
<h2>行恢复操作</h2>
<p>按照下面的方法,我们可以在回收站中的每一行数据加上一个恢复操作,方便恢复数据</p>
<p>先定义操作类<code>app/Admin/Actions/Post/Restore.php</code>:</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Actions\Post;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;
class Restore extends RowAction
{
protected $title = '恢复';
protected $model;
// 注意构造方法的参数必须要有默认值
public function __construct(string $model = null)
{
$this-&gt;model = $model;
}
public function handle(Request $request)
{
$key = $this-&gt;getKey();
$model = $request-&gt;get('model');
$model::withTrashed()-&gt;findOrFail($key)-&gt;restore();
return $this-&gt;response()-&gt;success('已恢复')-&gt;refresh();
}
public function confirm()
{
return ['确定恢复吗?'];
}
public function parameters()
{
return [
'model' =&gt; $this-&gt;model,
];
}
}</code></pre>
<p>添加到行操作:</p>
<pre><code class="language-php">use App\Models\Post;
use App\Admin\Actions\Post\Restore;
$grid-&gt;actions(function (Grid\Displayers\Actions $actions) {
if (request('_scope_') == 'trashed') {
$actions-&gt;append(new Restore(Post::class));
}
});</code></pre>
<h2>批量恢复操作</h2>
<p>先定义操作类<code>app/Admin/Actions/Post/BatchRestore.php</code>:</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Actions\Post;
use Dcat\Admin\Grid\BatchAction;
use Illuminate\Http\Request;
class BatchRestore extends BatchAction
{
protected $title = '恢复';
protected $model;
// 注意构造方法的参数必须要有默认值
public function __construct(string $model = null)
{
$this-&gt;model = $model;
}
public function handle(Request $request)
{
$model = $request-&gt;get('model');
foreach ((array) $this-&gt;getKey() as $key) {
$model::withTrashed()-&gt;findOrFail($key)-&gt;restore();
}
return $this-&gt;response()-&gt;success('已恢复')-&gt;refresh();
}
public function confirm()
{
return ['确定恢复吗?'];
}
public function parameters()
{
return [
'model' =&gt; $this-&gt;model,
];
}
}</code></pre>
<p>添加到批量操作:</p>
<pre><code class="language-php">use App\Models\Post;
use App\Admin\Actions\Post\BatchRestore;
$grid-&gt;batchActions(function (Grid\Tools\BatchActions $batch) {
if (request('_scope_') == 'trashed') {
$batch-&gt;add(new BatchRestore(Post::class));
}
});</code></pre>