我的应用程序中有一个主页,其中包含一系列我使用Blade语法将它们添加到页面中的块。所有这些小部件都使用HTML和Bootstrap样式。
每个块都存储在一个名为Widgets的文件夹中,基本上只是一堆HTML,如上所述。
E.g。 quick-search-form.blade.php
<div class="grid-item element-item" tabindex="9">
@include('widgets.events-cms')
</div>
我在窗口小部件库中有相同的块,用户应该可以从中选择,然后这些窗口小部件会出现在用户的主页上。
这是我可以用某些表做的事情吗?
我的概念
小部件表,其中包含#quick-search
等小工具的ID,基本上就是它。
public function up()
{
Schema::create('widgets', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('html_id');
$table->timestamps();
});
}
user_widget 表,其中包含 user_id , widget_id 和位置,表示订单为显示在页面上。
public function up()
{
Schema::create('users_widgets', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('widget_id');
$table->integer('position')->unique();
$table->timestamps();
});
}
然后,在Laravel中,我将用户的一对多设置为小部件。
/**
* Define that a User can have many widgets
*/
public function widgets()
{
return $this->hasMany(Widget::class, 'user_id', 'id');
}
然后我可以使用以下内容:
$widgets = $user->widgets
在视图中
@foreach($widgets as $widget)
$widget->id
@endforeach
我主要担心的是:
如何通过ID获取Widget的HTML? 当用户删除Widget时,我可以使用AJAX但是如何减少位置值?