Laravel-excel: [์งˆ๋ฌธ] ๊ตฌํ˜„ ๋œ ๊ฐ€์ ธ ์˜ค๊ธฐ ๋ฐฉ๋ฒ•์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2019๋…„ 01์›” 03์ผ  ยท  10์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Maatwebsite/Laravel-Excel

์ „์ œ ์กฐ๊ฑด

๋ฒ„์ „

  • PHP ๋ฒ„์ „ : 7.2
  • ๋ผ ๋ผ๋ฒจ ๋ฒ„์ „ : 5.6
  • ํŒจํ‚ค์ง€ ๋ฒ„์ „ : 3.1

๊ธฐ์ˆ 

๊ตฌํ˜„ ๋œ ๊ฐ€์ ธ ์˜ค๊ธฐ ๋ฉ”์„œ๋“œ์—์„œ ์ปจํŠธ๋กค๋Ÿฌ์™€ ํ†ต์‹  ํ•  ์ˆ˜์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ์— ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ์‹œ๋„ํ–ˆ์ง€๋งŒ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์— ๋Œ€ํ•œ ์–ธ๊ธ‰์„ ๋ณธ ์ ์ด ์—†์Šต๋‹ˆ๋‹ค ...

toCollection์„ ์‚ฌ์šฉํ•˜๊ณ  ์ „์ฒด ๊ฐ€์ ธ ์˜ค๊ธฐ๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ปฌ๋ ‰์…˜์œผ๋กœ ๋ฐ˜ํ™˜ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์ง€๋งŒ ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๊ฑฑ์ •๋ฉ๋‹ˆ๋‹ค.

question

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

class SalesImport implements ToCollection
{
    public $data;

    public function collection(Collection $rows)
    {
        .... lots of other stuff ....
        $this->data = $data;
    }
$import = new SalesImport;
Excel::import($import, 'imports/salesdata.xlsx');
dd($import->data);

๋ชจ๋“  10 ๋Œ“๊ธ€

๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ ์˜ค๊ธฐ ๊ฐœ์ฒด์˜ ํด๋ž˜์Šค ์†์„ฑ์— ๋„ฃ๊ณ  ๊ฐ€์ ธ ์˜ค๊ธฐ๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์™„๋ฃŒ๋œ ํ›„ ํ˜ธ์ถœ ํ•  ์ˆ˜์žˆ๋Š” getter๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์•„์ง ๋ญ”๊ฐ€ ๋น ์ง„ ๊ฒŒ ๋ถ„๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋ง์”€ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋‚ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๊ฐ€์ ธ ์˜ค๊ธฐ ํ˜ธ์ถœ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
$import = Excel::import(new SalesImport, 'imports/salesdata.xlsx');

class SalesImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        .... lots of other stuff ....
        $this->data = $data;
    }

์ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ์˜ $ import ๊ฐœ์ฒด๋กœ ๋Œ์•„ ๊ฐ€์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์ด ๋ถ„๋ช…ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์— ToCollection์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฒƒ์„ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๊นŒ?

class SalesImport implements ToCollection
{
    public $data;

    public function collection(Collection $rows)
    {
        .... lots of other stuff ....
        $this->data = $data;
    }
$import = new SalesImport;
Excel::import($import, 'imports/salesdata.xlsx');
dd($import->data);
class SalesImport implements ToCollection
{
    public $data;

    public function collection(Collection $rows)
    {
        .... lots of other stuff ....
        $this->data = $data;
    }

$import = new SalesImport; Excel::import($import, 'imports/salesdata.xlsx'); dd($import->data);

๋Œ€๋ฐ•!

class SalesImport implements ToCollection
{
    public $data;

    public function collection(Collection $rows)
    {
        .... lots of other stuff ....
        $this->data = $data;
    }
$import = new SalesImport;
Excel::import($import, 'imports/salesdata.xlsx');
dd($import->data);

https://docs.laravel-excel.com/3.1/imports/collection.html ๋˜๋Š” ๋ฌธ์„œ์˜ ๋‹ค๋ฅธ ์œ„์น˜์—์„œ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ๋งŽ์€ ์‹œ๊ฐ„์„ ์žƒ์—ˆ์Šต๋‹ˆ๋‹ค.

๋น„์Šทํ•œ ์˜ˆ๊ฐ€ ์ž ์‹œ ๋™์•ˆ ๋ฌธ์„œ์—์žˆ์—ˆ์Šต๋‹ˆ๋‹ค : https://docs.laravel-excel.com/3.1/architecture/objects.html#getters

ํ›Œ๋ฅญํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ,์ด ์˜ˆ์ œ๋Š” ๋‹จ์ผ ์‹œํŠธ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
WithMultipleSheets ๊ฐ€์ ธ ์˜ค๊ธฐ ๊ตฌํ˜„์— ๋Œ€ํ•œ ํŒ์ด ์žˆ์Šต๋‹ˆ๊นŒ? ํ–‰ ์ˆ˜๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ•ด์•ผํ•ฉ๋‹ˆ๊นŒ?

CRUD์—์„œ ๋‚˜๋Š” ์–ป๋Š”๋‹ค : ์ •์˜๋˜์ง€ ์•Š์€ ๋ฉ”์„œ๋“œ MaatwebsiteExcelExcel :: getRowCount () ํ˜ธ์ถœ

ํŠน์ • ์‹œํŠธ ๊ฐ€์ ธ ์˜ค๊ธฐ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

class ExcelSampleSheetsImport implements WithMultipleSheets 
{    
    protected $params; 

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

    public function sheets(): array
    {
        return [
            'Sheet to import' => new ExcelSampleImport($this->params)
        ];
    }
}

ExcelSampleImport ํด๋ž˜์Šค์—์„œ ๋ฌธ์„œ์— ์ง€์ •๋œ๋Œ€๋กœ ์นด์šดํ„ฐ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ์‹œํŠธ๋กœ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

ํด๋ž˜์Šค ๊ฐ€์ ธ ์˜ค๊ธฐ

class CommunityImport implements WithMultipleSheets
{   
    use WithConditionalSheets;

    public function conditionalSheets(): array
    {
        return [
            0 => new FirstSheetCommunityImport(),
            1 => new SecondSheetCommunityImport(),
        ];
    }   
}

์ฒซ ๋ฒˆ์งธ ์‹œํŠธ ํด๋ž˜์Šค

class FirstSheetCommunityImport implements ToCollection
{   
    public $id;

    public function collection(Collection $rows)
    {
    $i=0;
        foreach ($rows as $row) 
        {
            if($i==0){
            $this->id = $row[0];
            }
            $i++;
        }
    }    
}

ํŽ˜์ด์ง€ ์ปจํŠธ๋กค๋Ÿฌ-FirstSheetCommunityImport ํด๋ž˜์Šค์—์„œ ID์— ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒํ•ด์•ผํ•ฉ๋‹ˆ๊นŒ?

public function import()
     {
    $import = new CommunityImport();
    $import->onlySheets(0);
        Excel::import($import, storage_path('app/xls/').'comunidad-import.xlsx');
     } 

FirstSheetCommunityClass์—์„œ ํ•ด๋‹น ID์— ์•ก์„ธ์Šคํ•˜๋Š” ๊ฒƒ์€ ๋‚˜์—๊ฒŒ ๋ฌธ์ œ ์˜€์ง€๋งŒ Laravel ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 10 ๋ถ„ ๋™์•ˆ ๋‚ด ์ปฌ๋ ‰์…˜์„ ์ €์žฅ ํ•œ ๋‹ค์Œ Cache Facade๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์•ก์„ธ์Šคํ–ˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ์—ฌ๋Ÿฌ ์žฅ์˜ ๊ฒŒํ„ฐ์— ๋Œ€ํ•œ ์„ค๋ช…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ์œ„ํ•ด ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‚ด ์ƒ๊ฐ์— ์ข‹์€ ์ƒ๊ฐ์ด ์•„๋‹™๋‹ˆ๋‹ค ...

<?php
// VendorsExtraDataImportSheet.php:

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class VendorsExtraDataImportSheet implements ToCollection
{
    public $count = 0;

    public function collection(Collection $rows)
    {
            $this->count++;
    }
}
<?php
// VendorsExtraDataImport.php

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class VendorsExtraDataImport implements ToCollection, WithMultipleSheets
{
    use WithConditionalSheets;

    public const SHEET_TO_IMPORT = 'Example data input';

    public $sheet;

    public function collection(Collection $rows)
    {
    }


    public function conditionalSheets(): array
    {
        if (!$this->sheet) {
            $this->sheet = new VendorsExtraDataImportSheet;
        }

        return [
            self::SHEET_TO_IMPORT => $this->sheet,
        ];
    }
}
<?php
    // ...
    // PageController.php
    public function import()
    {
        $import = new VendorsExtraDataImport();
        $import->onlySheets(VendorsExtraDataImport::SHEET_TO_IMPORT);
        Excel::import($import, resource_path('data/vendors.xlsx'));
        dd($import->sheet->count);
    } 
    // ...
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰