#How should I define the exportAction exporter class in order to export hasMany relation?

4 messages · Page 1 of 1 (latest)

jagged elm
#

Use case:

  • I have a Product model, product has many prices.
    {
        return $this->hasMany(ProductPrice::class, 'product_id');
    }

I would like to export these prices in csv/excel format. Ideally add these values in separate columns:
SKU | PRODUCT_NAME | ..... | Price 1 | Price 2 |

Could you help me on this topic?

silver snowBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

dense glacier
#

There’s no good way to dynamically create columns in a csv since you have no way of knowing the number of columns needed. Also makes importing more difficult too. I would recommend exporting the relationship in one column as a json or array string.

jagged elm
#

Hello awcodes,
thanks, finally I imploded the prices into one field separated by a pipe, and import back this way.
The main problem here, that the getColumns function is static, and it's content can't be change based on options.