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

[Improvement] - relationship column with subfields #5613

Open
susanu opened this issue Aug 15, 2024 · 1 comment
Open

[Improvement] - relationship column with subfields #5613

susanu opened this issue Aug 15, 2024 · 1 comment
Labels

Comments

@susanu
Copy link
Contributor

susanu commented Aug 15, 2024

Hi guys,

I'm not sure where to write this but here is what i found recently (maybe others are facing this "issue" as well)

I have a HasMany relationship called shipments in one of my models.
In the ShowOperation i want to display a couple of attributes of this relationship.
Since the relationship has a LOT of entries (~1000) the page loads super slow.
I did some digging and found that changing the column type would improve the speed by A LOT.

I will post the column definition and the loading time of the page.
NOTICE: For this example, only this column is displayed so nothing else is impacting the speed of the page.

Column definition 1 - the most common i guess | ~4.4 seconds

CRUD::column('shipments')
->type('relationship')
->label(null)
->tab('Shipments')
->subfields([
    ['name' => 'id', 'type' => 'text', 'label' => 'ID'],
    ['name' => 'tracking_number', 'type' => 'text', 'label' => 'Tracking number'],
    ['name' => 'tracking_status', 'type' => 'text', 'label' => 'Tracking status'],
    ['name' => 'value', 'type' => 'text', 'label' => 'Value'],
    ['name' => 'collected_value', 'type' => 'text', 'label' => 'Collected'],
]);

Column definition 2 | ~1.15 seconds

CRUD::column('shipments')
    ->type('table')
    ->label(null)
    ->escaped(false)
    ->value(fn($entry) => $entry->shipments->toArray())
    ->columns([
        'id' => 'ID',
        'tracking_number' => 'Tracking Number',
        'tracking_status' => 'Tracking Status',
        'value' => 'Value',
        'collected_value' => 'Collected',
    ])
    ->tab('Shipments');

Column definition 3 - harcore | ~600ms

CRUD::column('shipments')
    ->type('table')
    ->label(null)
    ->escaped(false)
    ->value(fn($entry) => $entry->shipments->map(fn($shipment) => [
        'id' => $shipment->id,
        'tracking_number' => $shipment->tracking_number,
        'tracking_status' => $shipment->tracking_status,
        'value' => $shipment->value,
        'collected_value' => $shipment->collected_value,
    ])->all())
    ->columns([
        'id' => 'ID',
        'tracking_number' => 'Tracking Number',
        'tracking_status' => 'Tracking Status',
        'value' => 'Value',
        'collected_value' => 'Collected',
    ])
    ->tab('Shipments');

Cheers!

@jcastroa87
Copy link
Member

Hello @susanu

Thanks for sharing this, sure will help someone.

Cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants