Laravel 7如何获取父类别别名(嵌套的Elount)?

Laravel 7如何获取父类别别名(嵌套的Elount)?,laravel,eloquent,Laravel,Eloquent,在Laravel 7中,如何获取父类别别名 @foreach ($getAllWithPaginate as $item) <a href="{{ route('user.blog.showPost', [**question here (how get parent category alias?)**, $item->category->alias, $item->alias]) }}" class="for-posts-link">

在Laravel 7中,如何获取父类别别名

    @foreach ($getAllWithPaginate as $item)
       <a href="{{ route('user.blog.showPost', [**question here (how get parent category alias?)**, $item->category->alias, $item->alias]) }}" class="for-posts-link">
    @enforeach
我有两张桌子:

        Schema::create('blog_categories', function (Blueprint $table) {
            $table->id();

            $table->bigInteger('parent_id')->unsigned()->default(1);
            $table->string('alias')->unique();
            $table->string('title');
            $table->string('description')->nullable();

            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('blog_posts', function (Blueprint $table) {
            $table->id();

            $table->bigInteger('category_id')->unsigned();
            $table->bigInteger('user_id')->unsigned();

            $table->string('alias')->unique();
            $table->string('title');
            $table->text('fragment')->nullable();
            $table->text('content_html');

            $table->boolean('is_published')->default(false);
            $table->timestamp('published_at')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('category_id')->references('id')->on('blog_categories');
            $table->index('is_published');
        });

在模型中:

class BlogPost extends Model
{

    public function category()
    {
        return $this->belongsTo(BlogCategory::class);
    }

    public function parentCategory()
    {
        return $this->belongsTo(BlogCategory::class,
            'id', 'parent_id');
    }

}
路线:

Route::group(['namespace' => 'Blog', 'prefix' => 'blog'], function () {
    Route::get('posts', 'BlogPostController@index')->name('blog.posts');
    Route::get('{parentCategory}{category}/{alias}', 'BlogPostController@showPost')->name('user.blog.showPost');
});
在控制器中,我有以下方法:

    public function index()
    {
        $getAllWithPaginate = $this->blogPostRepository->getAllWithPaginate();

        return view('blog', compact('getAllWithPaginate'));
    }
在视图中,我可以访问$item->category->alias和$item->alias,但如何访问父类别别名

    @foreach ($getAllWithPaginate as $item)
       <a href="{{ route('user.blog.showPost', [**question here (how get parent category alias?)**, $item->category->alias, $item->alias]) }}" class="for-posts-link">
    @enforeach

类似于路由中的parentCategory->alias参数?谢谢你的帮助

我认为问题在于雄辩的命名惯例,有两种方法可以做到这一点。 1创造雄辩的属性 2.建立雄辩的关系,虽然你做到了,但可以通过更传统的方式来改善。 下面是我将如何做的示例

class BlogCategory extends Model
{
    public function parent()
    {
        return $this->belongsTo(BlogCategory::class,'parent_id', 'id');
    }
}
在视图中,我会像这样访问它

  {{ $item->category->parent->alias }}

希望这有帮助

谢谢!这是工作!我不知道我能做这个{{$item->category->parent->alias}