级联用户删除laravel

时间:2019-11-29 12:51:22

标签: php laravel

我正在尝试删除用户,但是我还需要删除ForeignKey个约束。

在我的用户模型中,我有

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    public $incrementing = false;

    protected $fillable = [
        'name', 
        'email', 
        'password',
        'timezone', 
        'profile_picture', 
        'notification_key'
    ];

    protected $hidden = [
        'password', 
        'pivot', 
        'admin'
    ];

    public static function boot()
    {
        parent::boot();

        static::creating(function ($instance) {
            $instance->id = Uuid::uuid4();
        });
    }

    public function groups()
    {
        return $this->belongsToMany(Group::class)
                    ->withPivot('user_role')
                    ->withTimestamps();        
    }

    public function events()
    {
        return $this->belongsToMany(Event::class);
    }

}

关系移民是

class CreateEventUserTable extends Migration
{

    public function up()
    {
        Schema::create('event_user', function (Blueprint $table) {
            $table->uuid('event_id');
            $table->uuid('user_id');
            $table->primary(['event_id', 'user_id']);

            $table->foreign('event_id')->references('id')->on('events');
            $table->foreign('user_id')->references('id')->on('users');
        });
    }


    public function down()
    {
        Schema::dropIfExists('event_user');
    }
}

class CreateGroupUserTable extends Migration
{

    {
        Schema::create('group_user', function (Blueprint $table) {
            $table->uuid('group_id');
            $table->uuid('user_id');
            $table->string('user_role')->nullable();
            $table->timestamps();
            $table->boolean('owner');
            $table->primary(['group_id', 'user_id']);

            $table->foreign('group_id')->references('id')->on('groups');
            $table->foreign('user_id')->references('id')->on('users');
        });
    }

    public function down()
    {
        Schema::dropIfExists('group_user');
    }
}

所以我试图删除这样的用户

public function delete($user) 
{
    $user = User::findOrfail('id', $user->id);
    $res = $user->groups()->events()->delete();

    if ($res) {
        return response('Success, user was deleted', 204);
    } else {
        return response()->json(error);
    }
}

但是我仍然收到

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`event_activities`, CONSTRAINT `event_activities_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: delete from `users` where `id` = someID) in file /home/server/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664 

我希望通过User模型中的关系方式进行删除,但是我仍然收到Integrity错误,那么这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

有3种方法可以实现:

  • 使用分离

    $user->groups()->events()->detach();
    $user->groups()->detach();
    
  • in this answer

  • 之类的相关模型上使用delete事件
  • 迁移。

    // in user_roles
    
    $table->integer('group_id');
    $table->foreign('group_id')->references("id")->on("groups")->onDelete("cascade");
    

    这翻译成:     在“网上论坛”上删除网上论坛“ id”后,请删除此行。

    将相同的内容应用于事件组。

编辑:

看着您的迁移,您实现了第3个解决方案,但您似乎忘记了

->onDelete('cascade');