Call Widget from PHP Variable instead of Hard-Coded String
paulrisk3 opened this issue · comments
Hello. My goal is to allow a user to select which widgets they want displayed in their dashboard, giving them a customizable experience. When the user logs off and logs back on, their widget selection settings should be the same (settings are saved in a database).
The issue I run into is when I try to call the selected widgets. I am returning an array of strings representing the title of the user's selected widgets, and for each of those strings, calling the widget by that name. The code looks like below:
{{-- My intended solution. Each $user_widget is just a string, like
'numUserErrors', 'numUserMissions', the names of my widgets. --}}
<div class="row">
@foreach ($user_widgets as $user_widget)
@widget({{$user_widget}})
@endforeach
</div>
{{-- Works, but won't allow user to select widgets --}}
<div class="row">
@widget('numUserErrors')
@widget('numUserMissions')
@widget('numTeamErrors')
</div>
The error I get with that @foreach, as written, is:
probably because the widget call is looking for the missing apostrophes. But when I add them (like so: @widget('{{$user_widget}}')), I get the error:
Is there a way to call a widget from a variable instead of a hard-coded string? Also, is there a better way to allow the user to select their own widgets that I've missed? Thanks!