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/PROPOSAL] Export original field instead of computed ones #55

Open skoyah opened 5 years ago

skoyah commented 5 years ago

Versions

Description

Resource index page displays a computed field from the original field. In this case, was adding an anchor tag to the email field using the Nova's method displayUsing().

Steps to Reproduce

Expected behavior:

Exporting to excel should use the original field instead of the computed one.

Actual behavior:

Exported field is exported with the computed format (eg. html tag)

Additional Information

Since it is usefull in some cases to export the computed fields, maybe we could have an option to toggle this behaviour either to a single or multiple columns.

patrickbrouwers commented 5 years ago

Currently we can't make the distinction between that. The goal of the package is to exactly export what is shown in the index view. I'm planning to look into a different way of selecting which fields should be in an export (something like ->onlyOnIndex but than exceptInExport and onlyInImport - Perhaps even a displayForExport()). Not sure if it's possible, but it's something I have on my roadmap to look into.

skoyah commented 5 years ago

Thanks for the feedback. I came up with a temporary solution. For anyone that might encounter the same thing in the near future:

Screenshot 2019-06-28 at 15 41 06

This way, by making the first email field 100% computed, the second field will the one being exported even when hidden.

potsky commented 4 years ago

Thanks for the feedback. I came up with a temporary solution. For anyone that might encounter the same thing in the near future:

Screenshot 2019-06-28 at 15 41 06

This way, by making the first email field 100% computed, the second field will the one being exported even when hidden.

Unfortunately this trick does not work anymore :-(

roarkmccolgan commented 4 years ago

This feature would be a great addition if possible.

techguydev commented 4 years ago

is there any solution for this?

fgilio commented 4 years ago

I just duplicate the fields and use this macros to decide when to show/use each:

Field::macro('onlyInExport', function () {
    // LaravelNovaExcel only uses fields that are visible in the index
    return $this
        // First hide it everywhere except on indexes
        ->onlyOnIndex()
        // Then decide when to show it when loaded an index
        ->showOnIndex(function (Request $request) {
            return $request instanceof \Maatwebsite\LaravelNovaExcel\Requests\ExportResourceActionRequest;
        });
});
Field::macro('showOnExport', function () {
    // LaravelNovaExcel only uses fields that are visible in the index
    return $this
        // In this case we don't care what other places the field is show
        ->showOnIndex(function (Request $request) {
            return $request instanceof \Maatwebsite\LaravelNovaExcel\Requests\ExportResourceActionRequest;
        });
});

I've actually been procrastinating on making a PR with this 2 macros, maybe someone can go ahead and do it. These have been very useful for me.