douao3636 2018-08-01 04:16
浏览 34
已采纳

Laravel Eager正在加载优化评论问题

So I was just messing with the eager loading stuff and I have been checking out how many Sql statements I have been pulling, I really feel what I have here does work but I feel like I could do this in a better way with eager loading. Does anyone have any suggestions or maybe a way to stop each comment from running a query?...

Here Is My Controller function called Show()

public function show($id)
{
    //
    $posts = Post::with(['user','comments'])->where('user_id', $id)->get();

    foreach($posts as $post) {
      echo "<hr /><h2>" . $post->title . "</h2>";
      echo "<p><u style='font-size: 0.8rem;'>By: ". $post->user->name ."</i></u> | <u style='font-size: 0.8rem;'>Date:" . $post->created_at->format('n-j-Y') . "</u>";
      echo "<br />" . $post->content . "</p><hr />";
      echo "<h3>Comments</h3>";
      if($post->comments->isNotEmpty()) {
        foreach($post->comments as $comment) {
          echo "<u>" . $comment->user->name . "</u><br />" . $comment->comment . "<br /><br />";
        }
      } else {
        echo "Sorry there are no comments yet.";
      }
    }
}

Here is the data it pulls and displays on the page I call. Please keep in mind I know I should use a view but I'm mostly doing this for the sake of learning.

select * from posts where user_id = ?

select * from users where users.id in (?)

select comments.*, taggables.post_id as pivot_post_id, taggables.taggable_id as pivot_taggable_id, taggables.taggable_type as pivot_taggable_type from comments inner join taggables on comments.id = taggables.taggable_id where taggables.post_id in (?, ?, ?) and taggables.taggable_type = ?

Posts Web Page:


Bad Ass Title

By: Moriah Mayer | Date:7-30-2018

Deserunt iure recusandae consequatur alias. Et non ratione unde deleniti. Est delectus recusandae consectetur quia tempore. Asperiores ea et voluptas molestias.


Comments

select * from users where users.id = ? limit 1

Julia Mante V

This is a random Post comment hope it works.

select * from users where users.id = ? limit 1

Moriah Mayer

This is another random Post comment hope it works.


New PHP Title

By: Moriah Mayer | Date:7-30-2018

This is our first content ever we created with out a tutorial.


Comments

select * from users where users.id = ? limit 1

Timmothy Fay DDS

That is a interesting post that you have here.


Moriahs Amazing Post

By: Moriah Mayer | Date:7-30-2018

Here we will be showing you what we can do with our mind set to the project there are so many things you can learn from this post.


Comments

Sorry there are no comments yet.

  • 写回答

1条回答 默认 最新

  • dounanyin3179 2018-08-01 04:20
    关注

    You also need to eager load the user related to each comment. You can eager load nested relationships using dot notation:

    $posts = Post::with(['user', 'comments.user'])->where('user_id', $id)->get();
    

    Now each comment will have its user eager loaded and will not run a new query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)