I have a situation in which a user has a homepage which will be full of widgets they can add or remove by clicking on them.
Each widget is essentially just a blade file stored under resources/views/widgets
An example:
@php
$widgetNo = 15;
$exist = false;
@endphp
@hasanyrole('News Contributor|News Approver')
<div class="grid-item element-item">
<section class="widget" id="articles-cms">
<div class="form-box widget light_blue">
<div class="row header">
<div class="col-xs-10">
<div class="heading">
<h2>News articles</h2>
</div>
</div>
<div class="col-xs-2 AddWidget">
@if(Route::current()->uri() == 'welcome')
<i class="fa fa-bars "></i>
@elseif(Route::current()->uri() == 'widget-library')
<form action="{{action('WidgetController@store')}}" method="POST">
@csrf
<input type="hidden" name="user_id" value="{{$user->id}}">
<input type="hidden" name="widgets" value="12">
<button type="submit">
<i class="fa fa-plus "></i>
</button>
</form>
@else @endif
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 col-md-12">
<p class="info">Create and edit News and updates</p>
<a href="/editable/news-and-updates" class="btn btn-filled-navy btn-full-width no-margin">Click Here</a>
</div>
</div>
</div>
</section>
</div>
@endrole
The reason for the PHP at the beginning is so that each widget has an ID I can work with, as you can see each widget also has a store and destroy form based on where they are within the application.
Structurally I have two tables: users
and widgets
The widgets
table looks like this:
In my User Model I have a one to many relation to Widgets that looks like this:
/**
* A user may have many Widgets
*
* @return void
*/
public function widgets()
{
return $this->hasMany(Widget::class, 'user_id', 'id')->orderBy('position', 'asc');
}
With this in place I can utilize the relation in the view.
I also have these two methods in the Widget model.
/**
* Get the user that has this Widget
*/
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
/**
* Check whether a given user has this Widget
*/
public function getIsSelected()
{
$selected = $this->user()->whereUserId(Auth::user()->id)->first();
return (!is_null($selected)) ? true : false;
}
This is so I can check whether a Widget has been selected by a user.
At the moment, in my homepage I just have a switch statement.
@foreach ($user->widgets as $widget)
@switch($widget->widget_id)
@case (1)
@include('widgets.quick-search-full-width')
@break;
@case(2)
@include('widgets.calendar')
@break;
@case (3)
@include('widgets.suggestion')
@break;
@case (4)
@include('widgets.task')
@break;
@case (5)
@include('widgets.quick-search')
@break;
@case (6)
@include('widgets.message')
@break;
@case(7)
@include('widgets.find-expert')
@break;
@case (8)
@include('widgets.holiday')
@break;
@case (9)
@include('widgets.expenses')
@break;
@case (10)
@include('widgets.snap-pole')
@break;
@case (11)
@include('widgets.teams-cms')
@break;
@case (12)
@include('widgets.events-cms')
@break;
@case (13)
@include('widgets.team-cms')
@break;
@case (14)
@include('widgets.article-todo')
@break;
@case (15)
@include('widgets.article-cms')
@break;
@case (16)
@include('widgets.vacancies')
@break;
@case(17)
@include('widgets.yammer')
@break;
@default
<h3>You currently have no widgets</h3>
@endswitch
@endforeach
My question is, is there a better way to pull in or use these widgets as obviously at the moment, displaying of Widgets is entirely dependant on having an ID within the widget using vanilla PHP.
you can have blade_name field in database so in that form for example you have 'calendar' in the record of that widget. so you can add your widgets like this
@foreach ($user->widgets as $widget)
@include('widgets.'.$widget->blade_name)
@endforeach