Php 如何访问另一个hasMany关系中的hasMany

Php 如何访问另一个hasMany关系中的hasMany,php,laravel,Php,Laravel,我怎样才能接触到另一个有很多口才的人?有3种模式:post、comment和upvote。每个帖子都有很多评论,每个评论都有很多投票 如果我删除帖子,我想删除评论以及评论的投票 $post = Post::find ($id); $post->comment->each->delete() // deletes all the post comment $post->delete() //delete the post 这是可行的,但我如何删除评论的upvote $po

我怎样才能接触到另一个有很多口才的人?有3种模式:post、comment和upvote。每个帖子都有很多评论,每个评论都有很多投票

如果我删除帖子,我想删除评论以及评论的投票

$post = Post::find ($id);
$post->comment->each->delete() // deletes all the post comment
$post->delete() //delete the post
这是可行的,但我如何删除评论的upvote

$post->comment->upvote->each->delete() // does not work to delete comment's upvote
这是行不通的。它回来了 消息:“属性[upvote]在此集合实例上不存在。”

更新

下表列出了这些方案

发布

Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('title');
$table->string('desc');
$table->string('category');
$table->integer('total_comment')->default(0);
$table->timestamps();
});
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->integer('post_id')->unsigned()->foreign('post_id')->references("id")->on("posts")->onDelete("cascade");
$table->string('comment');
$table->integer('upvote')->default(0);
$table->timestamps();
});
评论

Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('title');
$table->string('desc');
$table->string('category');
$table->integer('total_comment')->default(0);
$table->timestamps();
});
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->integer('post_id')->unsigned()->foreign('post_id')->references("id")->on("posts")->onDelete("cascade");
$table->string('comment');
$table->integer('upvote')->default(0);
$table->timestamps();
});
向上投票

Schema::create('upvote', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('comment_id')->unsigned()->foreign('comment_id')->references("id")->on("comments")->onDelete("cascade");
$table->integer('user_id');
$table->timestamps();
});

如果您的模型通过外键约束连接,
所有带有相关
注释的post
所有带有相关
投票的
注释将被删除,但必须包含
->onDelete('cascade')在迁移表中:

注释表迁移

$table->foreign('post_id')
  ->references('id')->on('posts')
  ->onDelete('cascade');
$table->foreign('comment_id')
  ->references('id')->on('comments')
  ->onDelete('cascade');
投票表迁移

$table->foreign('post_id')
  ->references('id')->on('posts')
  ->onDelete('cascade');
$table->foreign('comment_id')
  ->references('id')->on('comments')
  ->onDelete('cascade');
编辑:

另一种方法是获取
post
注释
,然后迭代每个
注释
以删除其
投票:

//Get the requierd post
$post = Post::find ($id);

//Iterate the requierd post comments
foreach ($post->comment as comment){
 //delete each comment upvotes
 comment->each->upvote->delete()
}

//then delete the comments
$post->comment->each->delete() // deletes all the post comment

最好在模型迁移中添加'onDelete('cascade'),我已经根据您的建议调整了表,但是当我执行$post->delete()时;只有帖子被删除,评论和投票仍然存在。调整表结构后是否运行了
php artisan migrate
?是的。尝试检查我的方案,我已经更新了上面的帖子,尝试将其定义为
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'),如果它仍然不起作用,您可以查看我编辑的答案以尝试其他方法。我必须在mysql中手动调整它以获取关系,因为架构没有构建它,所以没有生成外键,现在它可以工作了,谢谢