如何在 Employee 和 User 模型中使用 Softdeletes?

How can I use Softdeletes in the models of Employee and User?

员工移民

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 60);
        $table->string('phone_whats', 30)->nullable();
        $table->string('phone_home', 30)->nullable();
        $table->string('email', 255)->nullable();
        $table->string('dt_birthday', 20)->nullable();
        $table->string('zipcode', 20)->nullable();
        $table->integer('id_city')->unsigned();
        $table->integer('id_state')->unsigned();
        $table->string('address', 255)->comment('Endereço')->nullable();
        $table->string('number', 10)->nullable();
        $table->string('rg', 25)->nullable();
        $table->string('cpf', 20)->nullable();
        $table->string('password', 255)->nullable();
        $table->foreign('id_city')->references('id')->on('cities');
        $table->foreign('id_state')->references('id')->on('states');
        $table->timestamps();
        $table->softDeletes();
    });
}    
public function down()
{
    Schema::dropIfExists('employees');
}

用户迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('name',255);
        $table->integer('id_employee')->unsigned();
        $table->string('email',255)->unique();
        $table->string('password',255);
        $table->foreign('id_employee')->references('id')->on('employees');
        $table->timestamps();
        $table->softDeletes();
    });
}
public function down()
{
    Schema::dropIfExists('users');
}

我的模范员工

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Employee extends Model
{
    use SoftDeletes;
    protected $hidden = ['created_at', 'deleted_at', 'updated_at'];
    protected $dates = ['deleted_at'];
    protected $fillable = [
        'name', 'phone_home', 'phone_whats','email', 'dt_birthday', 'number', 'rg', 'cpf', 'address',
        'id_state', 'id_city', 'password'
    ];
}

我的模型用户

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
}

员工控制器

public function destroy(Employee $employee)
{
        $employee->delete();
        return redirect()->back();
}

我想在员工和用户的注册中使用软删除,当我只在员工控制器注册中测试时,我的数据库注册被删除了,我只想在员工和用户中应用软删除功能,将注册保留在数据库中,只需填写两个数据库的 delete_at 列,使它们处于非活动状态。

查看代码,它应该可以工作 - 您是否运行了迁移(如果您稍后添加了 softDeletes)?你怎么知道你的记录被删除了?你是直接在数据库里查的,还是通过Eloquent查的?由于 SoftDeletes trait 隐含地添加到每个查询子句中,只返回未被软删除的模型,因此通过 User::find() 你不会得到被软删除的模型。