dcat-admin

dcat-admin


数据软删除

<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">&amp;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-&amp;gt;id('ID')-&amp;gt;sortable(); $grid-&amp;gt;title('Title'); $grid-&amp;gt;created_at('Created at'); $grid-&amp;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-&amp;gt;filter(function () { // 范围过滤器,调用模型的`onlyTrashed`方法,查询出被软删除的数据。 $filter-&amp;gt;scope('trashed', '回收站')-&amp;gt;onlyTrashed(); });</code></pre> <p>在表头的筛选按钮的下拉菜单中就会出现一个<code>回收站</code>按钮,点击它,就会调用模型的<code>onlyTrashed</code>方法,从表中查询出被删除的数据,也就是回收站中的数据。</p> <p>&lt;img style=&quot;box-shadow:0 1px 6px 1px rgba(0, 0, 0, 0.12)&quot; width=&quot;40%&quot; src=&quot;{{public}}/assets/img/screenshots/trash-button.png&quot;&gt;</p> <h2>行恢复操作</h2> <p>按照下面的方法,我们可以在回收站中的每一行数据加上一个恢复操作,方便恢复数据</p> <p>先定义操作类<code>app/Admin/Actions/Post/Restore.php</code>:</p> <pre><code class="language-php">&amp;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-&amp;gt;model = $model; } public function handle(Request $request) { $key = $this-&amp;gt;getKey(); $model = $request-&amp;gt;get('model'); $model::withTrashed()-&amp;gt;findOrFail($key)-&amp;gt;restore(); return $this-&amp;gt;response()-&amp;gt;success('已恢复')-&amp;gt;refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' =&amp;gt; $this-&amp;gt;model, ]; } }</code></pre> <p>添加到行操作:</p> <pre><code class="language-php">use App\Models\Post; use App\Admin\Actions\Post\Restore; $grid-&amp;gt;actions(function (Grid\Displayers\Actions $actions) { if (request('_scope_') == 'trashed') { $actions-&amp;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">&amp;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-&amp;gt;model = $model; } public function handle(Request $request) { $model = $request-&amp;gt;get('model'); foreach ((array) $this-&amp;gt;getKey() as $key) { $model::withTrashed()-&amp;gt;findOrFail($key)-&amp;gt;restore(); } return $this-&amp;gt;response()-&amp;gt;success('已恢复')-&amp;gt;refresh(); } public function confirm() { return ['确定恢复吗?']; } public function parameters() { return [ 'model' =&amp;gt; $this-&amp;gt;model, ]; } }</code></pre> <p>添加到批量操作:</p> <pre><code class="language-php">use App\Models\Post; use App\Admin\Actions\Post\BatchRestore; $grid-&amp;gt;batchActions(function (Grid\Tools\BatchActions $batch) { if (request('_scope_') == 'trashed') { $batch-&amp;gt;add(new BatchRestore(Post::class)); } });</code></pre>

页面列表

ITEM_HTML