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

Use case: - I have a Product model, product has many prices.
public function prices(): HasMany
{
return $this->hasMany(ProductPrice::class, 'product_id');
}
public function prices(): HasMany
{
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?
3 Replies
awcodes
awcodes3mo ago
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.
Roland Barkóczi
Roland BarkócziOP3mo ago
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.

Did you find this page helpful?