【问题标题】:Laravel eloquent relationship and viewLaravel 雄辩的关系和看法
【发布时间】:2014-05-06 19:47:36
【问题描述】:

我正在尝试使用此线程中显示的表格作为示例来显示结构: Laravel Eloquent Filter By Column of Relationship

使用这个我想在一页(例如 index.blade.php)中显示如下内容:

Category 1
    Post1.Category1
    Post2.Category1
Category 2
    Post1.Category2
    Post2.Category2
    Post3.Category2
    Post4.Category2
Category 3    
    Post1.Category3

我不知道我需要从 Controller 传递哪些数组。

我可以得到类别列表,但是每个类别的帖子如何???

谢谢

任何想法

【问题讨论】:

  • 改写您的问题并格式化代码,以便我们知道您需要什么。

标签: laravel eloquent


【解决方案1】:

您只需要传递类别及其帖子。这很简单。

控制器

$categories = Category::with('posts')->get();
return View::make('some.view')->with('categories', $categories);

查看

@foreach($categories as $category)
    {{{ $category->name }}}
    @foreach($category->posts as $post)
        {{{ $post->name }}}.{{{ $category->name }}}
    @endforeach
@endforeach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-14
    • 2018-11-28
    • 2015-01-11
    • 2021-06-03
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多