laravel 软删除和恢复删除用户
laravel软删除和恢复删除用户
- 1 模型中指定软删除的标识字段
- 添加代码
- 文件
- 2 添加路由
- 3 应用方式
- 用 Laravel 自带的 Eloquent ORM 来实现软删除
- 生成表结构
1 模型中指定软删除的标识字段
添加代码
use Illuminate\Database\Eloquent\SoftDeletes;
// 调用定义的trait类 和 继承效果一样
use SoftDeletes;
// 软删除标识字段
protected $dates = ['deleted_at'];
文件
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
// 调用定义的trait类 和 继承效果一样
use SoftDeletes;
// 软删除标识字段
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
2 添加路由
Route::get('softDeletes', 'JuneController@softDeletes');
3 应用方式
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class JuneController extends Controller
{
public function softDeletes()
{
$user = User::find(request('id'));
// 软删除
// User::find(request('id'))->delete();
// 强制删除 在配置了软删除的时候,真实的删除操作
// User::find($id) -> forceDelete();
# withTrashed()显示所有的,包括已经进行了软删除的
// $user = User::withTrashed()->get();
// 还原
// $user = User::onlyTrashed()->where('id', request('id'))->restore();
return $user;
}
}
用 Laravel 自带的 Eloquent ORM 来实现软删除
首先在数据迁移文件中添加删除时间字段
D:\PHPstudyWWW\dcatAdmin\database\migrations\2022_04_06_084934_create_table_users.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableUsers extends Migration
{
# 用户表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
// 角色
$table->unsignedInteger('role_id')->default(0)->comment('角色ID');
$table->string('username', 50)->comment("账号");
$table->string('truename', 20)->default('未知')->comment("账号");
$table->string('password', 255)->comment('密码');
$table->timestamp('email_verified_at')->nullable();
$table->rememberToken();
$table->string('email', 50)->nullable()->comment('邮箱');
$table->string('phone', 15)->default('')->comment('手机号码');
$table->enum('sex', ['先生','女士'])->default('先生')->comment('性别');
$table->char('last_ip', 15)->default('')->comment('登录IP');
$table->timestamps();
// 实现软删除,需要添加delete_at字段,$table->softDeletes();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
生成表结构
PS D:\PHPstudyWWW\dcatAdmin> php artisan migrate --path=/database/migrations/2022_04_06_084934_create_table_users.php
Nothing to migrate.
PS D:\PHPstudyWWW\dcatAdmin>
laravel 执行 PHP migrate 报 Nothing to migrate.
laravel 会在你的数据库里面生成一个 migrations 表,这个表记录了你的 migrate 操作,当我们进行一些操作,比如手动删除表的时候,migrations 表里面的记录没有删除,手动删除,重新执行 php artisan migrate 命令就可以了。
PS D:\Centos\PHPstudyWWW\dcatAdmin> php artisan migrate --path=/database/migrations/2022_04_06_084934_create_table_users.php
Migrating: 2022_04_06_084934_create_table_users
Migrated: 2022_04_06_084934_create_table_users (0.08 seconds)
PS D:\Centos\PHPstudyWWW\dcatAdmin>
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`role_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '角色ID',
`username` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '账号',
`truename` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '未知' COMMENT '账号',
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密码',
`email_verified_at` timestamp NULL DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '邮箱',
`phone` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '手机号码',
`sex` enum('先生','女士') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '先生' COMMENT '性别',
`last_ip` char(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '登录IP',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
还没有评论,来说两句吧...