SpartnerNL / Laravel-Nova-Excel

🚀 Supercharged Excel exports for Laravel Nova Resources
https://docs.laravel-excel.com/nova/1.0/
MIT License
374 stars 73 forks source link

[QUESTION/FEATURE] Nova Import tool #86

Open ThaDaVos opened 4 years ago

ThaDaVos commented 4 years ago

Prerequisites

Description

There is progress being made for a Nova Import Tool. This issue is opened to serve as a point to ask questions.

PR: https://github.com/Maatwebsite/Laravel-Nova-Excel/pull/70 Twitter: https://twitter.com/patrickbrouwers/status/1143467691577171968

ThaDaVos commented 4 years ago

So I required the 2.0-dev version of the package with composer require maatwebsite/laravel-nova-excel:^2.0.x-dev

Now, how would one include the import tool so it can be used?

patrickbrouwers commented 4 years ago

I believe only the following steps are required:

ThaDaVos commented 4 years ago

Finally had the time again to work further on the project, but I think we're missing a step as right after changing my tools() inside my NovaServiceProvider.php to the following I'm getting an error as show below:


    /**
     * Get the tools that should be listed in the Nova sidebar.
     *
     * @return array
     */
    public function tools()
    {
        return [
            \Maatwebsite\LaravelNovaExcel\Tools\Import::class
        ];
    }

image

ThaDaVos commented 4 years ago

My fault, registering it wrong...

ThaDaVos commented 4 years ago

@patrickbrouwers is it possible to override which resource/model it uses? We've got the following relations List -> Data

And we want to run the action from the List resource, which only has a name field, create the needed Data models and link them to the list -> is this possible? Or should I copy the code in the package and extend on it?

patrickbrouwers commented 4 years ago

Don't think I have accounted for something like that.

ThaDaVos commented 4 years ago

as far as I can see in the code, you're not - still thinking about a way I can make it work -> maybe override the controller or something but I don't know how... -> maybe add some hooks/callbacks, just thinking out loud

patrickbrouwers commented 4 years ago

I wouldn't mind being able to configure the resource you want to import to on the action. Feel free to PR something like that.

ThaDaVos commented 4 years ago

I would first have to use a different of way of getting the source code then, maybe as a submodule, so I can change code - any tips?

patrickbrouwers commented 4 years ago

https://laravel-news.com/developing-laravel-packages-with-local-composer-dependencies

ThaDaVos commented 4 years ago

I'm getting a beautiful error: image

ThaDaVos commented 4 years ago

nvm... the issue is with how I setup my docker instance

ThaDaVos commented 4 years ago

I've got it partially working already, I can now call the action from my Lists resource and it fills the Data resource - still struggling to find a good way to hook into this so one can set attributes on the created models, for example to set the relations etc - also trying to find a way to supply extra information which can be used to create the List it self at the same time

Any idea's @patrickbrouwers ?

ThaDaVos commented 4 years ago

Big question @patrickbrouwers -> How does the ImportExcel action actually run? I don't see a handle function being specified

patrickbrouwers commented 4 years ago

It doesn't technically run the action (handle method), I only use the action to determine if it should show the button or not. Then it just goes via a controller call: https://github.com/Maatwebsite/Laravel-Nova-Excel/blob/2.0/src/Http/Controllers/UploadsImportsController.php#L47

ThaDaVos commented 4 years ago

I noticed - sorry for not mentioning it - I'm almost done, currently writing the after import hook - how can one get the models it created during the import after line https://github.com/Maatwebsite/Laravel-Nova-Excel/blob/2.0/src/Http/Controllers/UploadsImportsController.php#L47

ThaDaVos commented 4 years ago

Or do I need to use a different method on the importer for that? Maybe the toCollection?

ThaDaVos commented 4 years ago

Got that working too -> I added a hasMany relation on the Import model which connects with the resources->model:

    /**
     * @return HasMany
     */
    public function models()
    {
        $model = Nova::modelInstanceForKey($this->resource);

        return $this->hasMany(get_class($model));
    }
ThaDaVos commented 4 years ago

Got it fully working now! Don't know if it's meant this way, but I can import from one resource, fill another and then link it up together

ThaDaVos commented 4 years ago

I'll make a PR later on so you can see what I changed

ThaDaVos commented 4 years ago

My changes can be seen here: https://github.com/ThaDaVos/Laravel-Nova-Excel/commit/76a5176faf5eec98c81e5288f22a89d10291557c

ThaDaVos commented 4 years ago

@patrickbrouwers here's the promised Pull-Request: https://github.com/Maatwebsite/Laravel-Nova-Excel/pull/88

ThaDaVos commented 4 years ago

@patrickbrouwers I made some additional fixes regarding null values in excel sheets

patrickbrouwers commented 4 years ago

Hey thanks for the PR, I currently don't have a lot of time to look at it. Also releasing 2.0 will most likely also still take a while. Feel free to keep using your own fork, and if you have anymore improvements feel free to add them to the PR! :)

goromachine commented 3 years ago

Hi! any news on this?

patrickbrouwers commented 3 years ago

No sorry

goromachine commented 3 years ago

Thank you! I'm new to laravel and laravel nova, and I'm using this package to export my tables. Works really great :)

Do you have a work around or a recommendation to make imports? this really seems a good way to go but as an alternative, do you know any other package that makes import from the data exported from this one?

patrickbrouwers commented 3 years ago

There are a few packages that have used the Laravel Excel base package like this one: https://packagist.org/packages/anaseqal/nova-import You can have a look at packagist: https://packagist.org/?query=laravel%20nova%20import I haven't tried or used any of them, so I'm not sure which one is good and works.