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

Does not export columns with empty value #35

Closed
jfeid opened this issue Dec 14, 2018 · 5 comments
Closed

Does not export columns with empty value #35

jfeid opened this issue Dec 14, 2018 · 5 comments

Comments

@jfeid
Copy link

jfeid commented Dec 14, 2018

Hello, thanks for this great package.

When I am asking to export all attributes from a resource like follows:

public function actions(Request $request)
    {
        return [
            (new DownloadExcel())->allFields()->withHeadings();
        ];
    }

it fails to export attributes with empty value. This consequently mangles the export data with mismatched column/data.

For example, if the a resource in database has the following rows:

+----+------------+------+---------------------+---------------------+
| id | brand      | year | created_at          | updated_at          |
+----+------------+------+---------------------+---------------------+
|  1 | Alfa Romeo | 2018 | 2018-12-14 08:43:51 | 2018-12-14 08:43:51 |
|  2 | Audi       | NULL | 2018-12-14 08:44:10 | 2018-12-14 08:45:05 |
+----+------------+------+---------------------+---------------------+

and I am using the following Nova resource fields (you see Year is not an index field):

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Brand'),
            Number::make('Year')->onlyOnForms()
        ];
    }

will eventually export:

ID,Brand,year,created_at,updated_at
1,Alfa Romeo,2018,2018-12-14 08:43:51,2018-12-14 08:43:51
2,Audi,2018-12-14 08:44:10,2018-12-14 08:45:05

First row has 5 columns but 2nd row has 4 columns.

Debugging the code, I see that this line:

https://github.com/Maatwebsite/Laravel-Nova-Excel/blob/4ecc6c0f815da56e2874d15e79735734930fea0a/src/Actions/ExportToExcel.php#L306

prevents to export attribute if it is empty.

Similar problem is referenced in #18 and #19.

  • PHP v7.2
  • Laravel v5.6
  • Nova v1.2
  • Laravel-Nova-Excel v1.1.1
@laurencei
Copy link
Contributor

laurencei commented Dec 18, 2018

Yep - I've just run into this exact same issue.

(new DownloadExcel)->only('id', 'example', 'other')

If example column is blank, then other column data ends up in example column.

@laurencei
Copy link
Contributor

So following on from what @jfeid said - if you make this change, it seems to be fix the issue:

        foreach (array_diff($only, array_keys($row)) as $attribute) {
            if ($model->{$attribute}) {
                $row[$attribute] = $model->{$attribute};
            }
+         else {
+               $row[$attribute] = '';
+         }
        }

I'm just not sure of any side effects of that? It seems to solve it for me...?

@patrickbrouwers
Copy link
Member

Can you PR that.

@laurencei laurencei mentioned this issue Dec 27, 2018
@laurencei
Copy link
Contributor

@patrickbrouwers - done.

@patrickbrouwers
Copy link
Member

Fixed in latest release. Thanks @laurencei for the PR.

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