简体   繁体   English

Laravel-我应该存储模型结果以避免多个数据库查询吗?

[英]Laravel - should I store model results to avoid multiple database queries?

I am new to Laravel (5.1), and am wondering if I am not using model results in the right way. 我是Laravel(5.1)的新手,我想知道我是否没有以正确的方式使用模型结果。 Eg below is some of my code in a view. 例如,以下是我在视图中的一些代码。 I get the results I want, but the page currently loads quite slowly. 我得到了想要的结果,但是该页面当前加载非常缓慢。 I'm wondering if it's because it queries the database repeatedly/separately for every line below? 我想知道是否是因为它重复/分别查询下面的每一行? Eg should I store $item->sessions into a temporary PHP variable (say $x), then do $x->classes->some_column, etc? 例如,我应该将$ item-> sessions存储到一个临时PHP变量中(例如$ x),然后执行$ x-> classes-> some_column等吗?

Any advice greatly appreciated, thanks a lot in advance. 任何建议,不胜感激,非常感谢。

@foreach ($items as $item)
@if ($item->sessions->classes->some_column== 0)
{{$item->sessions->location}}
@else
{{$item->sessions->classes->suburb}}, {{$item->sessions->classes->state}}
@endif
Much more code that uses $item-> ...
@endforeach

感谢您提供的提示,我找到了Laravel 4-记录SQL查询 ,其中提供了有关如何监控查询数量的良好说明,以便我可以测试和优化自己

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM