Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 级联用户删除laravel_Php_Laravel - Fatal编程技术网

Php 级联用户删除laravel

Php 级联用户删除laravel,php,laravel,Php,Laravel,我正试图删除一个用户,但也需要删除ForeignKey约束 在我的用户模型中,我有 class User extends Authenticatable { use HasApiTokens, Notifiable; public $incrementing = false; protected $fillable = [ 'name', 'email', 'password', 'timezone'

我正试图删除一个用户,但也需要删除
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');
    }
}

所以我试着像这样删除用户

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 

我希望在用户模型中以按关系的方式进行删除,但我仍然收到一个完整性错误,那么正确的方法是什么呢?

有3种方法可以实现这一点:

  • 使用分离

    $user->groups()->events()->detach();
    $user->groups()->detach();
    
  • 在相关车型上使用删除事件,如

  • 迁移

    // in user_roles
    
    $table->integer('group_id');
    $table->foreign('group_id')->references("id")->on("groups")->onDelete("cascade");
    
    这意味着: 在“组”上删除组“id”时,删除此行

    对事件组应用相同的内容

编辑:

看看您的迁移,您实施了第三个解决方案,但您似乎忘记了


->onDelete('cascade')

你能展示你的迁移吗?好的@senty我已经添加了你错过的关系迁移
->onDelete('cascade')关于你的迁移,可能是这样吗?@senty,不是我喜欢的,我想了解他们在我之前做了什么,但谢谢你的澄清,我会玩的,我接受了你的回答。我很乐意澄清,请随意提问:)
// in user_roles

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