这段来源
Dcat admin
Dcat Admin是一个基于laravel-admin二次开发而成的后台系统构建工具,只需极少的代码即可快速构建出一个功能完善的高颜值后台系统。支持页面一键生成CURD代码,内置丰富的后台常用组件,开箱即用,让开发者告别冗杂的HTML代码,对后端开发者非常友好。
效果图
- 创建按钮
$grid->tools(function (Grid\Tools $tools) { | |
// excle 导入 | |
$tools->append(new Reast()); | |
}); | |
2.app/admin/actions/grid 下创建 Reast.php | |
namespace App\Admin\Actions\Grid; | |
use App\Admin\Actions\Form\Import; | |
use Dcat\Admin\Admin; | |
use Dcat\Admin\Actions\Response; | |
use Dcat\Admin\Grid\Tools\AbstractTool; | |
use Dcat\Admin\Traits\HasPermissions; | |
use Illuminate\Contracts\Auth\Authenticatable; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Http\Request; | |
class Reast extends AbstractTool | |
{ | |
/** | |
* @return string | |
*/ | |
protected $title = 'Title'; | |
public function render() | |
{ | |
$id = "reset-pwd-{$this->getKey()}"; | |
// 模态窗 | |
$this->modal($id); | |
return <<<HTML | |
<span class="grid-expand" data-toggle="modal" data-target="#{$id}"> | |
<a href="javascript:void(0)"><button class="btn btn-outline-info ">上传Excel并导入数据</button></a> | |
</span> | |
HTML; | |
} | |
protected function modal($id) | |
{ | |
$form = new Import(); | |
Admin::script('Dcat.onPjaxComplete(function () { | |
$(".modal-backdrop").remove(); | |
$("body").removeClass("modal-open"); | |
}, true)'); | |
// 通过 Admin::html 方法设置模态窗HTML | |
Admin::html( | |
<<<HTML | |
<div class="modal fade" id="{$id}"> | |
<div class="modal-dialog modal-lg" role="document"> | |
<div class="modal-content"> | |
<div class="modal-header"> | |
<h4 class="modal-title">导入数据</h4> | |
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> | |
</div> | |
<div class="modal-body"> | |
{$form->render()} | |
</div> | |
</div> | |
</div> | |
</div> | |
HTML | |
); | |
} | |
/** | |
* @param Model|Authenticatable|HasPermissions|null $user | |
* | |
* @return bool | |
*/ | |
protected function authorize($user): bool{ | |
return true; | |
} | |
/** | |
* @return array | |
*/ | |
protected function parameters() | |
{ | |
return []; | |
} | |
} | |
3.在Actions/form下创建Import.php | |
namespace App\Admin\Actions\Form; | |
use Dcat\Admin\Models\Administrator; | |
use Dcat\Admin\Widgets\Form; | |
use Symfony\Component\HttpFoundation\Response; | |
use App\Imports\DataExcel; | |
use Maatwebsite\Excel\Facades\Excel; | |
use Maatwebsite\Excel\Validators\ValidationException; | |
class Import extends Form | |
{ | |
public function handle(array $input) | |
{ | |
$file = env('APP_URL').'/upload/'.$input['file']; | |
try { | |
Excel::import(new DataExcel(time()), $input['file'],'public'); | |
//dcat-2.0版本写法 | |
return $this->response() | |
->success('导入成功') | |
->redirect('/'); | |
//dcat-1.7 | |
//return $this->success('导入成功'); | |
} catch (ValidationException $validationException) { | |
return Response::withException($validationException); | |
} catch (Throwable $throwable) { | |
//dcat 2.0写法 | |
$this->response()->status = false; | |
return $this->response()->error('上传失败')->refresh(); | |
//dcat 1.7 | |
//return $this->error('上传失败')->refresh(); | |
} | |
} | |
public function form() | |
{ | |
$this->file('file', '上传数据(Excel)')->rules('required', ['required' => '文件不能为空']); | |
} | |
} | |
再重复上一标题下的3.4方法