laravel 中模型预加载

  

  

    添加评论业务逻辑:
            
    (!!!不建议!!!)    
        直接在在模板中使用: 
        {{$post->comments  as  $ comment}}
        @foreach($post->comments as $comment)
        
        {{$comments->created_at}}    {{ $comment->user->name }}
            {{$comments->content }}
        
        @endforeach
            打破了 mvc 模式!!! 尽量去遵守 mvc 模式
    
    
        
        模型关联预加载:  优先
        
                $books = App\Book::with('author')->get();        author 加载。
                $books->load('author','publisher');                加载关联关系
                
        模板中:
                @foreach($post->comments as $comment)
                     {{$comment->created_at }}
                     {{$comment->user->name}}
                     {{$comment->content}}
                @endforeach
        
            先进行预加载则要先将模型预加载 , 在模板中不做查询操作。
            在渲染模板前,就把模板加载完了。而不是渲染模板之后。

 

posted @ 2017-08-22 12:36  silvercell  阅读(1630)  评论(0)    收藏  举报