合并儿童类Laravel的帖子

时间:2018-05-29 03:50:31

标签: 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__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');
 }

但是一个类别可能有儿童类别。当我显示类别父母时,我想获得所有儿童类别的帖子。这是我的脚本:

public function show_category($slug){

    $category = $this->category->findBySlug($slug);

    $childrens = $category->children;

    $posts = ...?

    return view('post.category.show',compact('posts','category'));
}

我不会将类别父类别和类别chidren的帖子合并到变量$ post。谁有好主意?

老兄,我的英语很糟糕。谢谢!

0 个答案:

没有答案