laravel-admin,Dcat-admin與maatwebsite/excel上傳

EricZhu發表於2021-06-29

準備工作

maatwebsite/excel

composer require maatwebsite/excel

laravel-admin

效果圖
在這裡插入圖片描述

在這裡插入圖片描述

  1. 建立按鈕
$grid->tools(function (Grid\Tools $tools) {
      // excle 匯入
      $tools->append(new ExcelAdd());
});

2.建立按鈕檔案

<?php

namespace App\Admin\Actions;

use Throwable;
use Encore\Admin\Admin;
use App\Imports\DataExcel;
use Encore\Admin\Actions\Action;
use Encore\Admin\Actions\Response;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\Validators\ValidationException;

class ExcelAdd extends Action
{
    protected $selector = '.import-template';

    public function handle()
    {
        try {
            Excel::import(new DataExcel(time()), request()->file('file'));
        } catch (ValidationException $validationException) {
            return Response::withException($validationException);
        } catch (Throwable $throwable) {
            $this->response()->status = false;
            return $this->response()->swal()->error($throwable->getMessage());
        }

        return $this->response()->success('上傳成功')->refresh();
    }

    // 按鈕樣式
    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default import-template">上傳簡歷</a>
HTML;
    }
    // 上傳表單
    public function form()
    {
        $this->file('file', '上傳簡歷')->rules('required', ['required' => '檔案不能為空']);
    }

    /**
     * @return string
     * 上傳效果
     */
    public function handleActionPromise()
    {
        $resolve = <<<'SCRIPT'
var actionResolverss = function (data) {
            $('.modal-footer').show()
            $('.tips').remove()
            var response = data[0];
            var target   = data[1];

            if (typeof response !== 'object') {
                return $.admin.swal({type: 'error', title: 'Oops!'});
            }

            var then = function (then) {
                if (then.action == 'refresh') {
                    $.admin.reload();
                }

                if (then.action == 'download') {
                    window.open(then.value, '_blank');
                }

                if (then.action == 'redirect') {
                    $.admin.redirect(then.value);
                }
            };

            if (typeof response.html === 'string') {
                target.html(response.html);
            }

            if (typeof response.swal === 'object') {
                $.admin.swal(response.swal);
            }

            if (typeof response.toastr === 'object') {
                $.admin.toastr[response.toastr.type](response.toastr.content, '', response.toastr.options);
            }

            if (response.then) {
              then(response.then);
            }
        };

        var actionCatcherss = function (request) {
            $('.modal-footer').show()
            $('.tips').remove()

            if (request && typeof request.responseJSON === 'object') {
                $.admin.toastr.error(request.responseJSON.message, '', {positionClass:"toast-bottom-center", timeOut: 10000}).css("width","500px")
            }
        };
SCRIPT;

        Admin::script($resolve);

        return <<<'SCRIPT'
         $('.modal-footer').hide()
         let html = `<div class='tips' style='color: red;font-size: 18px;'>匯入時間取決於資料量,請耐心等待結果不要關閉視窗!<img src="data:image/gif;base64,R0lGODlhEAAQAPQAAP///1VVVfr6+np6eqysrFhYWG5ubuPj48TExGNjY6Ojo5iYmOzs7Lq6utjY2ISEhI6OjgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAAFUCAgjmRpnqUwFGwhKoRgqq2YFMaRGjWA8AbZiIBbjQQ8AmmFUJEQhQGJhaKOrCksgEla+KIkYvC6SJKQOISoNSYdeIk1ayA8ExTyeR3F749CACH5BAkKAAAALAAAAAAQABAAAAVoICCKR9KMaCoaxeCoqEAkRX3AwMHWxQIIjJSAZWgUEgzBwCBAEQpMwIDwY1FHgwJCtOW2UDWYIDyqNVVkUbYr6CK+o2eUMKgWrqKhj0FrEM8jQQALPFA3MAc8CQSAMA5ZBjgqDQmHIyEAIfkECQoAAAAsAAAAABAAEAAABWAgII4j85Ao2hRIKgrEUBQJLaSHMe8zgQo6Q8sxS7RIhILhBkgumCTZsXkACBC+0cwF2GoLLoFXREDcDlkAojBICRaFLDCOQtQKjmsQSubtDFU/NXcDBHwkaw1cKQ8MiyEAIfkECQoAAAAsAAAAABAAEAAABVIgII5kaZ6AIJQCMRTFQKiDQx4GrBfGa4uCnAEhQuRgPwCBtwK+kCNFgjh6QlFYgGO7baJ2CxIioSDpwqNggWCGDVVGphly3BkOpXDrKfNm/4AhACH5BAkKAAAALAAAAAAQABAAAAVgICCOZGmeqEAMRTEQwskYbV0Yx7kYSIzQhtgoBxCKBDQCIOcoLBimRiFhSABYU5gIgW01pLUBYkRItAYAqrlhYiwKjiWAcDMWY8QjsCf4DewiBzQ2N1AmKlgvgCiMjSQhACH5BAkKAAAALAAAAAAQABAAAAVfICCOZGmeqEgUxUAIpkA0AMKyxkEiSZEIsJqhYAg+boUFSTAkiBiNHks3sg1ILAfBiS10gyqCg0UaFBCkwy3RYKiIYMAC+RAxiQgYsJdAjw5DN2gILzEEZgVcKYuMJiEAOwAAAAAAAAAAAA=="><\/div>`
         $('.modal-header').append(html)
process.then(actionResolverss).catch(actionCatcherss);
SCRIPT;
    }
}

3、獲取 excel 中第一個 檔案 sheet 中的資訊

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class DataExcel implements WithMultipleSheets
{
    private $round;

    public function __construct(int $round)
    {
        $this->round = $round;
    }

    public function sheets(): array
    {
        return [
            new FirstSheetImport($this->round),
        ];
    }
}

4、獲取資訊進行匯入資料庫

<?php

namespace App\Imports;

use App\Models\Data;
use App\Models\Data as DataModel;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::
    default('none');

class FirstSheetImport implements ToCollection, WithBatchInserts, WithChunkReading, WithHeadingRow, ToModel
{
    private $round;

    public function __construct(int $round)
    {
        $this->round = $round;
    }

    /**
     * @param array $row
     *
     * @return Model|Model[]|null
     */
    public function model(array $row)
    {
        // 斷資料是否
        $user = Data::where('mobile', '=', $row['手機'])->first();
        if ($user) {
            // 存在返回 null
            return null;
        }
        // 資料庫對應的欄位
        return new DataModel([
            'name' => $row['姓名'],
            'gender' => $row['性別'],
        ]);
    }

    public function collection(Collection $rows)
    {
        //
    }

    //批量匯入1000條
    public function batchSize(): int
    {
        return 1000;
    }

    //以1000條資料基準切割資料
    public function chunkSize(): int
    {
        return 1000;
    }
}

注意需要在models加上

 protected $fillable = ['img', 'content','static','username'];

這段來源

Dcat admin

Dcat Admin是一個基於laravel-admin二次開發而成的後臺系統構建工具,只需極少的程式碼即可快速構建出一個功能完善的高顏值後臺系統。支援頁面一鍵生成CURD程式碼,內建豐富的後臺常用元件,開箱即用,讓開發者告別冗雜的HTML程式碼,對後端開發者非常友好。
效果圖
在這裡插入圖片描述
在這裡插入圖片描述

  1. 建立按鈕
$grid->tools(function (Grid\Tools $tools) {
                // excle 匯入
                $tools->append(new Reast());
            });

2.app/admin/actions/grid 下建立 Reast.php

<?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">&times;</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

<?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方法

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章