【问题标题】:Delete all posts related to a user in laravel在 laravel 中删除与用户相关的所有帖子
【发布时间】:2018-11-12 05:55:44
【问题描述】:

这是我的帖子表

   public function up()
        {
            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('user_id');
                $table->integer('category_id')->unsigned()->index();
                $table->integer('photo_id')->default(0)->unsigned()->index();
                $table->string('title');
                $table->text('body');
                $table->timestamps();

                $table->foreign('user_id')
                    ->references('id')->on('users')
                    ->onDelete('cascade');


            });
        }

这是我的用户表

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->index()->unsigned()->nullable();
            $table->integer('photo_id')->index()->default(0);
            $table->boolean('is_active')->default(0);
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

这些是关系

 public function posts() {
        return $this->hasMany('App\Post');
    }

public function user() {
        return $this->belongsTo('App\User');
    }

删除用户代码

public function destroy($id)
    {
        $user = User::findOrFail($id);

        if($user->photo_id !== 0) {
            unlink(public_path() . $user->photo->path);
        }


        $user->delete();

        Session::flash('deleted_user', 'The user has been deleted.');

        return redirect('/admin/users');
    }

删除帖子代码

public function destroy($id)
    {
        $post = Post::findOrFail($id);

        if($post->photo_id !== 0) {
            unlink(public_path() . $post->photo->path);
        }


        $post->delete();

        return redirect('/admin/posts');

    }

我在删除用户时尝试删除与该用户相关的所有帖子。 为此,我在帖子表中使用了外部引用约束,如上所示 但是当我删除用户时它不起作用。帖子还在。 我不知道我做错了什么

【问题讨论】:

  • 你的删除代码是什么?
  • 见上文我在问题中添加了它
  • 你用的是什么版本的 Laravel,什么版本的 MySQL?

标签: php mysql laravel laravel-5 migration


【解决方案1】:

这个问题很可能是因为 MySQL 实例中的默认表引擎设置为不支持外键的 MyISAM。尝试在 MyISAM 表上使用外键绝对不是 Laravel 中的错误。虽然如果使用外键,Schema Builder 可以自动将引擎设置为 InnoDB,那就太好了。

所以,在你的架构中使用这一行

$table->engine = 'InnoDB';

或改变表格

ALTER TABLE table_name ENGINE=InnoDB;

可能对你有帮助。

【讨论】:

【解决方案2】:

为您创建自定义方法,例如 function destroyAllByUser()

并把代码像

DB::table('posts')->where('user_id', '=', 1)->delete();

希望对你有帮助

【讨论】:

  • 是的,我可以这样做,但如果我们可以使用 Laravel 已经提供给我们的东西会更好。
【解决方案3】:

删除用户;

public function destroy($id)
{
    $user = User::findOrFail($id);

    if($user->photo_id !== 0) {
        unlink(public_path() . $user->photo->path);
    }

    $user->posts->delete();

    $user->delete();

    Session::flash('deleted_user', 'The user has been deleted.');

    return redirect('/admin/users');
}

【讨论】:

    【解决方案4】:

    解决这个问题的另一种方法是在 laravel-project\config 文件夹下配置 database.php 文件以在 InnoDB 引擎上工作。

    'mysql' => [
       ...
    
       'engine' => 'InnoDB'
     ]
    

    现在您不用担心使用外键... 记住 - 如果您在创建表之前没有进行配置,您应该重新迁移。

    【讨论】:

      猜你喜欢
      • 2014-08-02
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多