Sparclex/nova-import-card

Extends BasicImporter

Closed this issue · 7 comments

Hi,

I would like to extends BasicImporter because i have 2 more fields in my database than in my Excel so i would like to extends BasicImporter and i did that:



php

namespace App\Imports;

use App\Statement;
use Sparclex\NovaImportCard\BasicImporter;

class StatementsImport extends BasicImporter {
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Statement|null
    */
    public function model(array $row)
    {
        return new Statement([
            'date_publishing'     => date("Y-m-d", strtotime($row[0])),
            'date_value'    => date("Y-m-d", strtotime($row[1])),
            'operation' => str_replace("\\n", "\\r", $row[2]), 
            'debit' => abs($row[3]),
            'credit' => abs($row[4]),
        ]);
    }
    
}

But i still have error regarding missing fields (pointable & pointable_type) ... Let me know if you have a good example about Extend BasicImporter .. i don't know which kind of informations you need in attributes or rules or regarding Model

my data in Excel (d/m/Y for date)
image

my date in database: (Y-m-d for date)

image

my Migration :

        Schema::create('statements', function (Blueprint $table) {
            $table->increments('id');
            $table->date('date_publishing');
            $table->date('date_value');
            $table->text('operation');
            $table->float('debit')->default(0);
            $table->float('credit')->default(0);
            $table->integer('pointable_id')->nullable();
            $table->string('pointable_type')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

my Model:

class Statement extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'statements';

    protected $fillable = ['date_publishing','date_value','operation','debit','credit'];
    
    //Casts of the model dates
    protected $casts = [
        'date_publishing' => 'date',
        'date_value' => 'date'
    ];

    public function pointable()
    {
        return $this->morphTo();
    }

Result:
image

Thanks in advance,
Rémy

What is your field definition in the nova resource?

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            MorphTo::make(__('Pointable'))->types([
                Provider::class,
                Client::class,
                User::class,
            ]),
            Date::make(__('Date Publishing'))->sortable(),
            Date::make(__('Date Value'))->sortable(),
            Text::make(__('operation'))
                ->resolveUsing(function ($name) {
                    return strtoupper($name);
                }),
            Number::make(__('debit'))->step(0.01)->sortable(),
            Number::make(__('credit'))->step(0.01)->sortable(),
            BelongsToMany::make(__('Documents')),    
        ];
    }

Try adding ->nullable to the morphTo field as described in the nova documentation: https://nova.laravel.com/docs/1.0/resources/relationships.html#morphto

I added:

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            MorphTo::make(__('Pointable'))->types([
                Provider::class,
                Client::class,
                User::class,
            ])
                ->nullable(),
            Date::make(__('Date Publishing'))->sortable(),
            Date::make(__('Date Value'))->sortable(),
            Text::make(__('operation'))
                ->resolveUsing(function ($name) {
                    return strtoupper($name);
                }),
            Number::make(__('debit'))->step(0.01)->sortable(),
            Number::make(__('credit'))->step(0.01)->sortable(),
            BelongsToMany::make(__('Documents')),    
        ];
    }

and now:

image

The Basic importer implements the WithHeadingRow interface by default (https://laravel-excel.maatwebsite.nl/3.1/imports/heading-row.html). So you need to access the rows by their header. In your case 'date_publishing' => date("Y-m-d", strtotime($row['Date'])),

       return new Statement([
            'date_publishing' => date("Y-m-d", strtotime($row['date'])),
            'date_value'    => date("Y-m-d", strtotime($row['valeur'])),
            'operation' => str_replace("\\n", "\\r", $row['libelle']), 
            'debit' => abs(intval(str_replace($symbols, '', $row['debit']))),
            'credit' => abs(intval(str_replace($symbols, '', $row['credit']))),
        ]);

now it works ! so far so good ! thanks !