Php 合并儿童类Laravel的帖子
我创建了一个表来保存帖子和类别的关系Php 合并儿童类Laravel的帖子,php,laravel,Php,Laravel,我创建了一个表来保存帖子和类别的关系 Schema::create('post__post_category_relations', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->integer('post_id')->unsinged(); $table->integer('category_id')->unsinged();
Schema::create('post__post_category_relations', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('post_id')->unsinged();
$table->integer('category_id')->unsinged();
});
这是分类表:
Schema::create('post__categories', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('parent_id')->nullable();
$table->timestamps();
});
我用关系雄辩来获得所有类别的帖子。这是类别模型:
public function posts()
{
return $this->belongsToMany(Post::class, 'post__post_category_relations', 'category_id');
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id');
}
但是一个类别可能有childrens类别。我想得到所有的儿童类职位时,我显示类别的家长。以下是我的脚本:
public function show_category($slug){
$category = $this->category->findBySlug($slug);
$childrens = $category->children;
$posts = ...?
return view('post.category.show',compact('posts','category'));
}
我不想将父类和子类的帖子合并到变量$post。有人有好主意吗
先生,我的英语太差了。谢谢 可能重复的谢谢,我会看到这个帖子!显示类别表和模型哦,这个toturial告诉我们如何获取子类别,但是我想将所有子类别的帖子都放到一个变量中。你能帮我吗?