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
user6122500
  • 892
  • 1
  • 15
  • 31
  • 1
    Can you show how you get content of $items variable please. – Autista_z Jun 05 '16 at 10:43
  • use eager loadinf if you didnt already, to avoid having too much querys `$items = Item::with('sessions.classes')->get()` – Achraf Khouadja Jun 05 '16 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? – Ryan Vincent Jun 05 '16 at 15:19

1 Answers1

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

Community
  • 1
  • 1
user6122500
  • 892
  • 1
  • 15
  • 31