๊ตฌํ ๋ ๊ฐ์ ธ ์ค๊ธฐ ๋ฉ์๋์์ ์ปจํธ๋กค๋ฌ์ ํต์ ํ ์์๋ ๋ฐฉ๋ฒ์ด ์๋์ง ๊ถ๊ธํฉ๋๋ค. ์ปจํธ๋กค๋ฌ์ ๋ฐ์ดํฐ ๋ฐฐ์ด์ ๋ฐํํ๋ ค๊ณ ์๋ํ์ง๋ง ํต๊ณผํ์ง ๋ชปํ์ต๋๋ค. ์ด๊ฒ์ ๋ํ ์ธ๊ธ์ ๋ณธ ์ ์ด ์์ต๋๋ค ...
toCollection์ ์ฌ์ฉํ๊ณ ์ ์ฒด ๊ฐ์ ธ ์ค๊ธฐ๋ฅผ ์ปจํธ๋กค๋ฌ์ ์ปฌ๋ ์ ์ผ๋ก ๋ฐํ ํ ์ ์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ์ฑ๋ฅ ์ ํ๊ฐ ๊ฑฑ์ ๋ฉ๋๋ค.
๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ค๊ธฐ ๊ฐ์ฒด์ ํด๋์ค ์์ฑ์ ๋ฃ๊ณ ๊ฐ์ ธ ์ค๊ธฐ๊ฐ ์ปจํธ๋กค๋ฌ์์ ์๋ฃ๋ ํ ํธ์ถ ํ ์์๋ 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);
}
// ...
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ