【问题标题】:Laravel 8 : SoftDeletes not updated when deleting a user linked to a formLaravel 8:删除链接到表单的用户时未更新 SoftDeletes
【发布时间】:2022-01-05 09:51:11
【问题描述】:

当我删除一个用户时,我在 Laravel 8 上遇到问题我的 'deleted_at' 已在我的 'user' 表中更新,但我的 'forms' 表中的 'deleted_at' 没有更新,因为我想删除链接的表单给这个用户,否则我有一个错误,因为当他不再存在时我会显示他的信息。 请问如何解决这个问题?

我在模型中使用了软删除 当我删除表单时,“deleted_at”会更新。

用户迁移:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('firstname');
            $table->string('lastname');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->foreignId('current_team_id')->nullable()->onDelete('cascade');
            $table->string('profile_photo_path', 2048)->nullable();
            $table->timestamps();
            $table->foreignId('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->softDeletes();
        });
    }

public function down()
    {
        Schema::dropIfExists('users', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }

表格迁移:

public function up()
    {
        Schema::create('forms', function (Blueprint $table) {
            $table->id();
            $table->string('title', 100);
            $table->text('message');
            $table->datetime('date');
            $table->string('color');
            $table->softDeletes();
            $table->timestamps();
        });
    }


    public function down()
    {
        // Schema::dropIfExists('forms');
        Schema::table('forms', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }

将用户添加到表单迁移:

public function up()
    {
        Schema::table('forms', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }


    public function down()
    {
        Schema::table('forms', function (Blueprint $table) {
            $table->dropForeign('user_id')->onDelete('cascade');
        });
    }

【问题讨论】:

  • 你能显示删除代码,它没有正确反映在数据库中吗?
  • 在我的控制器文件中销毁方法:``` public function destroy($id) { if (!Auth::check()) { return redirect('login'); } $users = 用户::findOrFail($id); $用户->删除();返回重定向()->路由('admin'); } ```

标签: laravel laravel-8 database-migration soft-delete


【解决方案1】:

软删除不适用于级联外键。级联外键是sql 服务器的一项功能,软删除是laravel 的一项功能,需要laravel 上下文才能工作。

当您删除它时,要使软删除起作用,您需要在关系的每个模型上调用 delete。这对于集合和高阶函数来说非常容易。

{
    $user = User::firstOrFail($id);

    $user->forms->each->delete();
    $user->delete();

    ...
}

【讨论】:

    【解决方案2】:

    非常感谢先生, 多亏了你,我学到了很多!

    对于那些想要控制器中的功能的人,这使得可以删除所属的用户,因此 id 并因此在我的情况下也删除了与他相关的表单(模型中的函数用于基数实体之间):

    public function destroy($id)
        {
            // $users = User::findOrFail($id);
            // $users->delete();
    
            $user = User::findOrFail($id);
            $user->forms->each->delete();
            $user->delete();
    
            return redirect()->route('admin');
        }
    
    • findOrFail 而不是 firstOrFail ^^'

    【讨论】:

    • 如果我的回答有帮助,请接受它,将答案添加到新答案而不是更新您的问题不符合规范?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多