One solution would be to store the query result in a session variable and use it to generate the drop down menu. Here's an example implementation in Laravel:
- In your login controller, retrieve the query result and store it in a session variable:
// Retrieve the query result
$menuItems = DB::table('menu_items')->get();
// Store it in a session variable
session(['menuItems' => $menuItems]);
- In your view, check if the session variable exists and use it to generate the drop down menu:
@if(session()->has('menuItems'))
<select>
@foreach(session('menuItems') as $menuItem)
<option value="{{ $menuItem->id }}">{{ $menuItem->name }}</option>
@endforeach
</select>
@endif
- To ensure that the session variable is cleared when the user logs out, add the following code to your logout controller:
session()->forget('menuItems');
This approach ensures that the query is only executed once per session, reducing unnecessary database queries.