Dcat Admin
Dcat Admin是一個基於laravel-admin二次開發而成的後臺系統構建工具,只需極少的程式碼即可快速構建出一個功能完善的高顏值後臺系統。支援頁面一鍵生成CURD程式碼,內建豐富的後臺常用元件,開箱即用,讓開發者告別冗雜的HTML程式碼,對後端開發者非常友好。
參考連結:blog.csdn.net/qq_42468039/article/...
本次實現的匯入功能是Dcat的版本為”dcat/laravel-admin”: “2.0.9-beta”,
實現效果
1.安裝maatwebsite/excel
composer require maatwebsite/excel
2.在控制器中新增按鈕
use App\Admin\Actions\Modal\memberModal;
$grid->tools(function (Grid\Tools $tools) {
//Excel匯入
$tools->append(new memberModal());
});
3.建立檔案
app/admin/actions/Modal/memberModal.php
<?php
namespace App\Admin\Actions\Modal;
use App\Admin\Actions\Form\memberForm;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class memberModal 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 memberForm();
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 [];
}
}
app/admin/actions/Form/memberForm.php
<?php
namespace App\Admin\Actions\Form;
use App\Admin\Actions\Imports\memberImport;
use Dcat\Admin\Widgets\Form;
use Maatwebsite\Excel\Facades\Excel;
class memberForm extends Form
{
public function handle(array $input)
{
try {
//上傳檔案位置,這裡預設是在storage中,如有修改請對應替換
$file = storage_path('/app/public/' . $input['file']);
Excel::import(new memberImport(), $file);
return $this->response()->success('資料匯入成功')->refresh();
} catch (\Exception $e) {
return $this->response()->error($e->getMessage());
}
}
public function form()
{
$this->file('file', '上傳資料(Excel)')->rules('required', ['required' => '檔案不能為空'])->move('admin/upload/');
}
}
app/admin/actions/Imports/memberImport.php
<?php
namespace App\Admin\Actions\Imports;
use App\Models\Member;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
class memberImport implements ToModel, WithStartRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
// 0代表的是第一列 以此類推
// $row 是每一行的資料
//查詢是否存在,存在就不寫入
$user = Member::where('username', '=', $row[0])->first();
if ($user) {
return null;
}
return new Member([
'username' => $row[0],
'name' => $row[1],
]);
}
/**
* 從第幾行開始處理資料 就是不處理標題
* @return int
*/
public function startRow(): int
{
return 2;
}
}
4.Model允許寫入
class Member extends Model
{
protected $fillable = ['name','username'];
}
5.匯入Excel樣式
本作品採用《CC 協議》,轉載必須註明作者和本文連結