如何解决如何在员工和用户模型中使用软删除?
员工迁移
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();
}
我想在员工和用户的注册中使用softdeletes,当我只在Employee Controller register中测试时,我的数据库注册被删除了,我只想在同一个函数中在员工和用户中应用softdeletes,将注册保留在数据库中,只需填写两个数据库的 delete_at 列,使它们处于非活动状态。
解决方法
查看代码,它应该可以工作 - 您是否运行过迁移(如果您稍后添加了 softDeletes
)?你怎么知道你的记录被删除了?您是直接在数据库中检查还是通过 Eloquent 进行检查?由于 SoftDeletes
trait 隐含地添加到每个查询子句中,只返回没有被软删除的模型,所以通过 User::find()
你不会得到被软删除的模型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。