Importer Not Working
Hi , inside MedicalSourceResource class , no data imported i got notification with processing , aslo i have been checked the import table for issues and it not errors
Has One discount relation ,
public static function table(Table $table): Table
{
return $table
->headerActions([
ImportAction::make()
->importer(MedicalSourceImporter::class)
])
public static function table(Table $table): Table
{
return $table
->headerActions([
ImportAction::make()
->importer(MedicalSourceImporter::class)
])
#IMPORTER
class MedicalSourceImporter extends Importer
{
protected static ?string $model = MedicalSource::class;
public static function getColumns(): array
{
return [
ImportColumn::make('specialist_id')
->numeric()
->rules(['integer']),
ImportColumn::make('type')
->relationship(),
ImportColumn::make('name')
->rules(['max:255']),
// etc ... etc ...
ImportColumn::make('discounts.xrays')
->label(__('xrays_discount'))
->rules(['max:100']),
ImportColumn::make('discounts.pathology')
->label(__('pathology'))
->rules(['max:100']),
ImportColumn::make('discounts.local')
->label(__('local'))
->rules(['max:100']),
#IMPORTER
class MedicalSourceImporter extends Importer
{
protected static ?string $model = MedicalSource::class;
public static function getColumns(): array
{
return [
ImportColumn::make('specialist_id')
->numeric()
->rules(['integer']),
ImportColumn::make('type')
->relationship(),
ImportColumn::make('name')
->rules(['max:255']),
// etc ... etc ...
ImportColumn::make('discounts.xrays')
->label(__('xrays_discount'))
->rules(['max:100']),
ImportColumn::make('discounts.pathology')
->label(__('pathology'))
->rules(['max:100']),
ImportColumn::make('discounts.local')
->label(__('local'))
->rules(['max:100']),
public function handleRecordCreation(MedicalSource $medicalSource, array $record): void
{
$discountData = [
'statement' => $record['statement'],
'surgical' => $record['surgical'],
'emergency' => $record['emergency'],
'labs' => $record['labs'],
'xrays' => $record['xrays'],
'internal' => $record['internal'],
'other' => $record['other'],
'pathology' => $record['pathology'],
'local' => $record['local'],
'imported' => $record['imported'],
'cosmetic' => $record['cosmetic'],
];
$medicalSource->discounts->create($discountData);
}
public function handleRecordCreation(MedicalSource $medicalSource, array $record): void
{
$discountData = [
'statement' => $record['statement'],
'surgical' => $record['surgical'],
'emergency' => $record['emergency'],
'labs' => $record['labs'],
'xrays' => $record['xrays'],
'internal' => $record['internal'],
'other' => $record['other'],
'pathology' => $record['pathology'],
'local' => $record['local'],
'imported' => $record['imported'],
'cosmetic' => $record['cosmetic'],
];
$medicalSource->discounts->create($discountData);
}
1 Reply
solved finally
public function resolveRecord(): ?MedicalSource
{
$medicalSource = new MedicalSource();
// Define the keys for discounts data
$discountsKeys = ['statement', 'surgical', 'emergency', 'labs', 'internal', 'other', 'xrays', 'pathology', 'local', 'imported', 'cosmetic'];
// Initialize the discounts data array
$discountsData = [];
// Loop through the discounts keys
foreach ($discountsKeys as $key) {
// If the key exists in $this->data, add it to $discountsData and remove it from $this->data
if (isset($this->data[$key])) {
$discountsData[$key] = $this->data[$key];
unset($this->data[$key]);
}
}
// Fill the MedicalSource instance with data and save it
$medicalSource->fill($this->data)->save();
// Create the Discounts record
$medicalSource->discounts()->create($discountsData);
return $medicalSource;
}
public function resolveRecord(): ?MedicalSource
{
$medicalSource = new MedicalSource();
// Define the keys for discounts data
$discountsKeys = ['statement', 'surgical', 'emergency', 'labs', 'internal', 'other', 'xrays', 'pathology', 'local', 'imported', 'cosmetic'];
// Initialize the discounts data array
$discountsData = [];
// Loop through the discounts keys
foreach ($discountsKeys as $key) {
// If the key exists in $this->data, add it to $discountsData and remove it from $this->data
if (isset($this->data[$key])) {
$discountsData[$key] = $this->data[$key];
unset($this->data[$key]);
}
}
// Fill the MedicalSource instance with data and save it
$medicalSource->fill($this->data)->save();
// Create the Discounts record
$medicalSource->discounts()->create($discountsData);
return $medicalSource;
}