0

I am new to Laravel (5.1), and am wondering if I am not using model results in the right way. E.g. 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? E.g. should I store $item->sessions into a temporary PHP variable (say $x), then do $x->classes->some_column, etc?

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
3
  • 1
    Can you show how you get content of $items variable please. Commented Jun 5, 2016 at 10:43
  • use eager loadinf if you didnt already, to avoid having too much querys $items = Item::with('sessions.classes')->get() Commented Jun 5, 2016 at 12:32
  • Please, imo, You need to get some measurements of what is taking the time. Currently it is just speculation. i.e. 1) define slowly? 2) How many items are you showing on a page? 3) How many items are you fetching from the database each time? Have you benchmarked the individual queries to find out which are the slow ones? Are you pushing a lot of data to the client (i.e. is latency a factor)? Without the measurement then you may spend a lot of time changing things that have not much impact on the elapsed time? Commented Jun 5, 2016 at 15:19

1 Answer 1

0

Thanks for the tips guys I found Laravel 4 - logging SQL queries which has good instructions on how to monitor the number of queries so I can test and optimise myself

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.