Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.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_Mysql_Laravel_Laravel 5_Migration - Fatal编程技术网

Php 删除与laravel中用户相关的所有帖子

Php 删除与laravel中用户相关的所有帖子,php,mysql,laravel,laravel-5,migration,Php,Mysql,Laravel,Laravel 5,Migration,这是我的表 public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('user_id'); $table->integer('category

这是我的表

   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');

    }
当我删除一个用户时,我试图删除与该用户相关的所有帖子。 为此,我在posts表中使用了foreignreferenceconstraint,如上所示 但当我删除用户时,它不起作用。这些职位仍然存在。
我不知道我做错了什么

这个问题很可能是因为MySQL实例中的默认表引擎被设置为MyISAM,它不支持外键。尝试在MyISAM表上使用外键绝对不是Laravel中的错误。尽管如果使用外键,模式构建器可以自动将引擎设置为InnoDB会更好

因此,在您的模式中使用这一行

$table->engine = 'InnoDB';
或者把桌子改成

ALTER TABLE table_name ENGINE=InnoDB;

可能会对您有所帮助。

创建自定义方法,如
函数destroyAllByUser()

然后把代码

DB::table('posts')->where('user_id', '=', 1)->delete();
我希望它可以帮助删除用户

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');
}

另一种解决方法是在laravel project\config文件夹下配置database.php文件,以便在InnoDB引擎上工作

'mysql' => [
   ...

   'engine' => 'InnoDB'
 ]
现在,当您使用外键时,无需担心。。。
请记住-如果您在创建表之前没有配置此选项,您应该再次进行重新排序。

您的删除代码是什么?请参见上文我在问题中添加的删除代码。请查看以下答案:您使用的是什么版本的Laravel和什么版本的MySQL?是,我可以这样做,但是如果我们能够使用Laravel已经提供给我们的东西会更好。但是InnoDB和MyISAM之间有什么区别,它会如何影响我的应用程序检查:和