Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION/FEATURE] Nova Import tool #86

Open
2 of 3 tasks
ThaDaVos opened this issue Apr 7, 2020 · 28 comments
Open
2 of 3 tasks

[QUESTION/FEATURE] Nova Import tool #86

ThaDaVos opened this issue Apr 7, 2020 · 28 comments

Comments

@ThaDaVos
Copy link

ThaDaVos commented Apr 7, 2020

Prerequisites

  • Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • Checked that your issue isn't already filed.
  • Checked if no PR was submitted that fixes this problem.

Description

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

PR: #70
Twitter: https://twitter.com/patrickbrouwers/status/1143467691577171968

@ThaDaVos
Copy link
Author

ThaDaVos commented Apr 7, 2020

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
Copy link
Member

I believe only the following steps are required:

  • Add the Maatwebsite\LaravelNovaExcel\Tools\ Import to your tools
  • Add the BelongsToImport to your model
  • Add the ImportExcel action to your resource actions
  • Run migrations

@ThaDaVos
Copy link
Author

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
Copy link
Author

My fault, registering it wrong...

@ThaDaVos
Copy link
Author

@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
Copy link
Member

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

@ThaDaVos
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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?

@ThaDaVos
Copy link
Author

I'm getting a beautiful error:
image

@ThaDaVos
Copy link
Author

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

@ThaDaVos
Copy link
Author

ThaDaVos commented Apr 15, 2020

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
Copy link
Author

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

@patrickbrouwers
Copy link
Member

patrickbrouwers commented Apr 15, 2020

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
Copy link
Author

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
Copy link
Author

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

@ThaDaVos
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

@ThaDaVos
Copy link
Author

My changes can be seen here: ThaDaVos@76a5176

@ThaDaVos
Copy link
Author

@patrickbrouwers here's the promised Pull-Request: #88

@ThaDaVos
Copy link
Author

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

@patrickbrouwers
Copy link
Member

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
Copy link

Hi! any news on this?

@patrickbrouwers
Copy link
Member

No sorry

@goromachine
Copy link

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants