Laravel中的分页和即时加载

Laravel中的分页和即时加载,laravel,pagination,eager-loading,Laravel,Pagination,Eager Loading,我渴望加载这个,它的工作很好。但是现在我想把这个分页。我试过了 {{dd($topics->toArray())}并返回以下内容: My Controller method is: $topics=Topic::with(array('subtopics'=>function($query){ $query->orderBy('glc_subtopic_priority','asc'); }))->with('subtopics.res

我渴望加载这个,它的工作很好。但是现在我想把这个分页。我试过了
{{dd($topics->toArray())}
并返回以下内容:

My Controller method is:

$topics=Topic::with(array('subtopics'=>function($query){
            $query->orderBy('glc_subtopic_priority','asc');
        }))->with('subtopics.resources')->find($id)->paginate(1);
        return View::make('subtopics.index')->with('topics', $topics);
问题是当我显示数据时。我正在使用

array (size=7)
  'total' => int 2
  'per_page' => int 1
  'current_page' => int 1
  'last_page' => int 2
  'from' => int 1
  'to' => int 1
  'data' => 
    array (size=1)
      0 => 
        array (size=5)
          'id' => int 1
...........
@foreach($topics->subtopics作为$topic)
  • {{$topics->glc_subtopic_name}
  • {{$topic->glc_subtopic_content} @foreach($topic->resources as$resource)
    {{$resource->glc_subtopic_resource_url} @endforeach @endforeach
    这将返回错误:
    未定义属性:照亮\Pagination\Paginator::$subtopics


    如何访问分页变量
    $topics

    中的数据如果需要指定
    子主题的查询
    ,则应该从该查询中急切地加载
    资源
    模型,而不是再次急切地加载
    子主题

    试试这个:

    @foreach($topics->subtopics as $topic)
        <li>{{ $topics->glc_subtopic_name }}</li>
            {{ $topic->glc_subtopic_content }}
            @foreach($topic->resources as $resource)
            <br/>{{ $resource->glc_subtopic_resource_url }}
        @endforeach
    @endforeach
    

    是我干的。我想给这些子主题分页,而不是主题。为此,我将paginate函数放入:Topic::with(['subtopics'=>function($query){$query->with('resources')->orderBy('glc_subtopic_priority','asc')->paginate(1);}])->find($id);我现在无法访问$topics->links()。有什么方法可以做到这一点吗?嗯,我没有尝试过类似的方法,但是
    links()
    将是一种分页关系的方法。所以,可能是
    $topics->subtopics->links()
    ,但既然您只查找一个特定主题,为什么不直接查询您的subtopics并将其传递给视图呢<代码>$subtopics=Subtopic::with('resources')->orderBy('glc_Subtopic_priority','sac')->where('topic_id',$id)->分页(10)/
    返回视图::make('subtopics.index',['subtopics'=>$subtopics])/
    {{$subtopics->links()}
    我尝试了$topics->subtopics->links()。但是$topics->subtopics是一个集合。我按照你的建议做了。现在可以了!非常感谢。
    $topics = Topic::with(['subtopics' => function($query){
        $query->with('resources')->orderBy('glc_subtopic_priority','asc');
    }])->find($id)->paginate(1);
    
    return View::make('subtopics.index', ['topics' => $topics]);