column('id', 'ID')->sortable(); $grid->column('pet.name', '宠物名称'); $grid->column('battle_type', '战斗类型')->display(function ($value) { $types = [ PetBattleLog::BATTLE_TYPE_HARVEST => '偷菜', PetBattleLog::BATTLE_TYPE_GUARD => '守护', PetBattleLog::BATTLE_TYPE_ROYALE => '争霸赛', ]; return $types[$value] ?? '未知'; }); $grid->column('opponent.name', '对手宠物')->display(function ($value) { return $value ?: '无对手'; }); $grid->column('result', '战斗结果')->display(function ($value) { $results = [ PetBattleLog::RESULT_FAIL => '失败', PetBattleLog::RESULT_SUCCESS => '胜利', ]; return $results[$value] ?? '未知'; })->label(); $grid->column('reward', '战斗奖励')->display(function ($value) { if (is_array($value) || is_object($value)) { return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } return $value ?: '无奖励'; }); $grid->column('battle_time', '战斗时间')->sortable(); $grid->column('created_at', '创建时间'); // 筛选 $grid->filter(function ($filter) { $helper = new FilterHelper($filter, $this); $helper->equal('id', 'ID'); $helper->equalSelectModelPet('pet_id', '宠物'); $filter->equal('battle_type', '战斗类型')->select([ PetBattleLog::BATTLE_TYPE_HARVEST => '偷菜', PetBattleLog::BATTLE_TYPE_GUARD => '守护', PetBattleLog::BATTLE_TYPE_ROYALE => '争霸赛', ]); $filter->equal('result', '战斗结果')->select([ PetBattleLog::RESULT_FAIL => '失败', PetBattleLog::RESULT_SUCCESS => '胜利', ]); $filter->between('battle_time', '战斗时间')->datetime(); }); // 默认按战斗时间倒序排序 $grid->model()->orderBy('battle_time', 'desc'); return $grid; }); } /** * 详情页 * * @param mixed $id * @return Show */ protected function detail($id) { return Show::make($id, new PetBattleLogRepository(['pet', 'opponent']), function (Show $show) { $helper = new ShowHelper($show, $this); $helper->field('id', 'ID'); $show->field('pet.name', '宠物名称'); $show->field('pet.user_id', '用户ID'); $show->field('battle_type', '战斗类型')->as(function ($value) { $types = [ PetBattleLog::BATTLE_TYPE_HARVEST => '偷菜', PetBattleLog::BATTLE_TYPE_GUARD => '守护', PetBattleLog::BATTLE_TYPE_ROYALE => '争霸赛', ]; return $types[$value] ?? '未知'; }); $show->field('opponent.name', '对手宠物')->as(function ($value) { return $value ?: '无对手'; }); $show->field('result', '战斗结果')->as(function ($value) { $results = [ PetBattleLog::RESULT_FAIL => '失败', PetBattleLog::RESULT_SUCCESS => '胜利', ]; return $results[$value] ?? '未知'; }); $show->field('reward', '战斗奖励')->json(); $show->field('battle_time', '战斗时间'); $show->field('created_at', '创建时间'); return $show; }); } /** * 表单 * * @return Form */ protected function form() { return Form::make(new PetBattleLogRepository(), function (Form $form) { $helper = new FormHelper($form, $this); $form->display('id', 'ID'); $helper->selectModelPet('pet_id', '宠物') ->required(); $form->select('battle_type', '战斗类型') ->options([ PetBattleLog::BATTLE_TYPE_HARVEST => '偷菜', PetBattleLog::BATTLE_TYPE_GUARD => '守护', PetBattleLog::BATTLE_TYPE_ROYALE => '争霸赛', ]) ->required(); $helper->selectModelPet('opponent_id', '对手宠物') ->help('可以为空,表示没有对手的战斗'); $form->select('result', '战斗结果') ->options([ PetBattleLog::RESULT_FAIL => '失败', PetBattleLog::RESULT_SUCCESS => '胜利', ]) ->required(); $helper->embedsCats('reward', '战斗奖励') ->help('战斗奖励,JSON格式'); $form->datetime('battle_time', '战斗时间') ->default(now()) ->required(); $form->display('created_at', '创建时间'); return $form; }); } }