Multiple columns from the same relationship in an ExportAction

Heya everyone! I am trying to create an export, and I need to get two columns computed aggregating the same relationship, with a different scope.
For example: User has a relationship with Post (1:N). Post has a type field that can either be 'video' or 'image'. In my User export, I'd like to have two columns "has videos" and "has images".
I can do one just fine doing something like
ExportColumn::make('posts_exists')
->exists(['posts' => fn (Builder $query) => $query->where('type', 'video')])
->label('Has videos')
ExportColumn::make('posts_exists')
->exists(['posts' => fn (Builder $query) => $query->where('type', 'video')])
->label('Has videos')
But if I try to add another for the images I need to use the same column name (posts_exists) and it breaks. Is there any way to get it done? For now I've resorted to creating two "scoped" relationships, but I'd like to avoid it if possible.
Solution:
In the end I went with the custom relationships, I only had two so it was not that bad.
Jump to solution
3 Replies
Umar Farooq
Umar Farooq5mo ago
maybe use
ExportColumn::make('id')
->exists(['posts' => fn (Builder $query) => $query->where('type', 'image')])
->label('Has images')
ExportColumn::make('id')
->exists(['posts' => fn (Builder $query) => $query->where('type', 'image')])
->label('Has images')
Daniele
DanieleOP4mo ago
That would not work, the aggregated field gets labeled "posts_exists", giving it the name "id" would show the id not the result of the exists.
Solution
Daniele
Daniele4mo ago
In the end I went with the custom relationships, I only had two so it was not that bad.
Want results from more Discord servers?
Add your server