model()->with(['category', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems']);
$grid->column('id', 'ID')->sortable();
$grid->column('name', '商品名称');
$grid->column('image', '商品图片')->image('', 50, 50);
$grid->column('category.name', '所属分类');
$grid->column('category_name', '分类名称');
// 消耗组列 - 可点击跳转
$grid->column('consumeGroup.name', '消耗组')->display(function ($name) {
if (!$name || !$this->consume_group_id) {
return '无';
}
return $name;
})->link(function () {
if (!$this->consume_group_id) {
return '';
}
return admin_url('game-consume-groups/' . $this->consume_group_id);
});
// 奖励组列 - 可点击跳转
$grid->column('rewardGroup.name', '奖励组')->display(function ($name) {
if (!$name || !$this->reward_group_id) {
return '无';
}
return $name;
})->link(function () {
if (!$this->reward_group_id) {
return '';
}
return admin_url('game-reward-groups/' . $this->reward_group_id);
});
// 消耗组详情列
$grid->column('consume_group_details', '消耗组详情')->display(function () {
if (!$this->consumeGroup) {
return '无消耗组';
}
return $this->consumeGroup->formatConsumeDetails();
})->width('200px');
// 奖励组详情列
$grid->column('reward_group_details', '奖励组详情')->display(function () {
if (!$this->rewardGroup) {
return '无奖励组';
}
return $this->rewardGroup->formatRewardDetails();
})->width('200px');
$grid->column('max_buy', '购买限制')->display(function ($maxBuy) {
return $maxBuy > 0 ? $maxBuy : '无限制';
});
$grid->column('sort_order', '排序权重')->sortable();
$grid->column('is_active', '状态')->switch();
$grid->column('start_time', '上架时间')->sortable();
$grid->column('end_time', '下架时间')->sortable();
// 过滤器
$grid->filter(function (Grid\Filter $filter) {
$filter->equal('id', 'ID');
$filter->like('name', '商品名称');
$filter->equal('category_id', '所属分类')->select(
ShopCategory::pluck('name', 'id')
);
$filter->like('category_name', '分类名称');
$filter->equal('consume_group_id', '消耗组')->select(
GameConsumeGroup::pluck('name', 'id')
);
$filter->equal('reward_group_id', '奖励组')->select(
GameRewardGroup::pluck('name', 'id')
);
$filter->equal('is_active', '状态')->select([
0 => '禁用',
1 => '启用',
]);
$filter->between('start_time', '上架时间')->datetime();
$filter->between('end_time', '下架时间')->datetime();
});
// 工具栏
$grid->toolsWithOutline(false);
$grid->disableViewButton();
$grid->showQuickEditButton();
$grid->enableDialogCreate();
$grid->enableDialogEdit();
$grid->setDialogFormDimensions('800px', '720px');
});
}
/**
* 创建详情页
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new ShopItemRepository(), function (Show $show) {
$show->field('id', 'ID');
$show->field('name', '商品名称');
$show->field('description', '商品描述');
$show->field('image', '商品图片')->image();
$show->field('category.name', '所属分类');
$show->field('category_name', '分类名称');
$show->field('consumeGroup.name', '消耗组')->as(function ($name) {
return $name ?: '无';
});
$show->field('rewardGroup.name', '奖励组')->as(function ($name) {
return $name ?: '无';
});
$show->field('max_buy', '购买限制')->as(function ($maxBuy) {
return $maxBuy > 0 ? $maxBuy : '无限制';
});
$show->field('sort_order', '排序权重');
$show->field('is_active', '状态')->as(function ($isActive) {
return $isActive ? '启用' : '禁用';
});
$show->field('start_time', '上架时间');
$show->field('end_time', '下架时间');
$show->field('created_at', '创建时间');
$show->field('updated_at', '更新时间');
});
}
/**
* 创建表单
*
* @return Form
*/
protected function form()
{
return Form::make(new ShopItemRepository(), function (Form $form) {
$form->display('id', 'ID');
$form->text('name', '商品名称')->required();
$form->textarea('description', '商品描述')->rows(3);
$form->image('image', '商品图片')->autoUpload()->uniqueName()->help('建议尺寸:200x200');
$form->select('category_id', '所属分类')
->options(ShopCategory::pluck('name', 'id'))
->required();
$form->text('category_name', '分类名称')->help('字符串格式的分类名称,区别于上面的分类ID');
$form->select('consume_group_id', '消耗组')
->options(GameConsumeGroup::pluck('name', 'id'))
->help('选择购买此商品需要消耗的资源组');
$form->select('reward_group_id', '奖励组')
->options(GameRewardGroup::pluck('name', 'id'))
->help('选择购买此商品获得的奖励组');
$form->number('max_buy', '购买限制')->min(0)->default(0)->help('0表示无限制');
$form->number('sort_order', '排序权重')->default(0)->help('数字越小越靠前');
$form->switch('is_active', '状态')->default(true);
$form->datetime('start_time', '上架时间')->help('留空表示不限制上架时间');
$form->datetime('end_time', '下架时间')->help('留空表示不限制下架时间');
$form->display('created_at', '创建时间');
$form->display('updated_at', '更新时间');
});
}
}