Home  >  Article  >  PHP Framework  >  Let’s talk about the soft delete function in Laravel Admin

Let’s talk about the soft delete function in Laravel Admin

PHPz
PHPzOriginal
2023-04-09 08:30:02908browse

Laravel Admin is a very popular open source backend management framework with many convenient components and plug-ins available for use. Among them, the soft delete function is one of the indispensable functions for many developers, which can achieve elegant data deletion and recovery. This article will introduce the soft deletion function in Laravel Admin, hoping to help readers better master this aspect of knowledge.

First of all, what is soft deletion? In traditional data deletion, we usually completely delete data from the database through physical deletion, which may cause some unnecessary problems. Such as accidentally deleting data, being unable to recover data, etc. Soft deletion means that the data is no longer physically deleted in the database, but a column deleted_at is added to the data table. When the data needs to be deleted, the value of the column will be set to the current time, which is equivalent to This data is marked as deleted. In this way, even if the data is deleted, we can still retrieve the deleted data by querying deleted_at for data that is not empty.

The soft deletion function in Laravel Admin is very easy to implement. You only need to create a deleted_at data table field. In the model, we need to use the soft deletion trait, that is, use SoftDeletes;, to enable soft deletion, as shown below:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    protected $dates = [&#39;deleted_at&#39;];

    // ...
}

There is a $dates attribute here , which means that this field should be added to the carbon instantiated object to facilitate us to format and operate time.

When we use Laravel Admin's data table component, it will automatically filter out soft deleted data. If we need to view soft-deleted data, we only need to check "Show deleted data" in the filter conditions above the data table.

Soft deletion of data is also very easy in Laravel Admin. Just call the delete() method in the controller. This method will set the deleted_at field to the current time, marking the data as deleted. If we need to use the restore function, we can call the restore() method to set the deleted_at field of the deleted data to null.

public function destroy($id)
{
    Product::destroy($id); // 执行软删除

    return redirect(&#39;/admin&#39;)->with(['success' => '删除成功']);
}

public function restore($id)
{
    $product = Product::withTrashed()->find($id);
    $product->restore(); // 还原删除数据

    return redirect('/admin')->with(['success' => '还原成功']);
}

In short, the soft deletion function in Laravel Admin brings us great convenience and flexibility. It can help us avoid some operational difficulties and security issues, making our development more efficient and reliable. I hope readers can master the content introduced in this article and apply it to the soft deletion function in actual development.

The above is the detailed content of Let’s talk about the soft delete function in Laravel Admin. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn