Replies: 2 comments 2 replies
-
I figured, it would be the cleanest way if app/Extends/Maatwebsite/Excel/TransformRows.php namespace App\Extends\Maatwebsite\Excel;
interface TransformRows
{
public function transformRows(array $rows): array;
} app/Extends/Maatwebsite/Excel/Sheet.php namespace App\Extends\Maatwebsite\Excel;
use Maatwebsite\Excel\Sheet as ExcelSheet;
class Sheet extends ExcelSheet
{
public $test = 'test';
private array $rows = [];
/**
* @param object $import
* @param int|null $startRow
* @param null $nullValue
* @param bool $calculateFormulas
* @param bool $formatData
*
* @return array
*/
public function toArray($import, int $startRow = null, $nullValue = null, $calculateFormulas = false, $formatData = false)
{
$rows = parent::toArray($import, $startRow, $nullValue, $calculateFormulas, $formatData);
if ($import instanceof TransformRows) {
$rows = $import->transformRows($rows);
}
$this->rows = $rows;
return $rows;
}
} app/Extends/Maatwebsite/Excel/Reader.php namespace App\Extends\Maatwebsite\Excel;
use Maatwebsite\Excel\Reader as BaseReader;
use App\Extends\Maatwebsite\Excel\Sheet;
use Maatwebsite\Excel\Concerns\SkipsUnknownSheets;
use Maatwebsite\Excel\Exceptions\SheetNotFoundException;
class Reader extends BaseReader
{
/**
* @param $import
* @param $sheetImport
* @param $index
*
* @return Sheet|null
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws SheetNotFoundException
*/
protected function getSheet($import, $sheetImport, $index)
{
try {
return Sheet::make($this->spreadsheet, $index);
} catch (SheetNotFoundException $e) {
if ($import instanceof SkipsUnknownSheets) {
$import->onUnknownSheet($index);
return null;
}
if ($sheetImport instanceof SkipsUnknownSheets) {
$sheetImport->onUnknownSheet($index);
return null;
}
throw $e;
}
}
} app/Providers/AppServiceProvider.php namespace App\Providers;
use App\Extends\Sheet as ExtendsSheet;
use App\Extends\Maatwebsite\Excel\Reader as ExtendsReader;
use Illuminate\Support\ServiceProvider;
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Files\Filesystem;
use Maatwebsite\Excel\QueuedWriter;
use Maatwebsite\Excel\Sheet;
use Maatwebsite\Excel\Writer;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind('excel', function ($app) {
return new Excel(
$app->make(Writer::class),
$app->make(QueuedWriter::class),
$app->make(ExtendsReader::class),
$app->make(Filesystem::class)
);
});
}
} |
Beta Was this translation helpful? Give feedback.
-
You can't use onRow and Model together, you have to pick either one. When using onRow, you have to handle saving the model yourself. If you use Model, you can return as many model instances as you want (so return 3 models there). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have an excel spreadsheet like the following:
That results in nine models with that attributes:
I tried to find a solution but I'm stuck:
Beta Was this translation helpful? Give feedback.
All reactions