Ad

Laravel Very Short Term Cache?

- 1 answer

In a Laravel blade view, templates like this:

@foreach($events as $event)
  <p>{{ $event->user->name }}</p>
@endforeach

results in SQL queries like this when viewing the page:

select * from `event_logs`
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1
select * from `users` where `users`.`id` = 1 limit 1

Is there a way to tell it to remember the result of that user query within just that one page request?

i.e. not any of the medium-term or long-term caching modules which require you to keep track of when data has changed.

Ad

Answer

You can try selecting data with eager loading by using "with" in your query to get data at same time when you are fetching data for the parent table.

For example;

$books = App\Book::with('author')->get();
Ad
source: stackoverflow.com
Ad