Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.11k stars 890 forks source link

[Bug] relationship_count causes 500 #5527

Closed zeknoss closed 4 months ago

zeknoss commented 4 months ago

Bug report

What I did

I added a column in the list operation controller that simply views the count of a relationship of a certain model.

What I expected to happen

It should list the rows with the relevant count column in the view.

What happened

It worked until some time, then started giving 500 error. The error details is not logged in any place (enabling debug mode does not work).

What I've already tried to fix it

I tried to create another attribute that simply gives the relationship count and added it as a text column instead. It worked for some time and then 500 started again.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug is still there.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

PHP 8.2.13 (cli) (built: Nov 24 2023 13:10:01) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.13, Copyright (c) Zend Technologies with Zend OPcache v8.2.13, Copyright (c), by Zend Technologies

LARAVEL VERSION:

10.37.2.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.2.2 backpack/crud: 6.7.14 backpack/logmanager: v5.0.1 backpack/pro: 2.1.2 backpack/theme-coreuiv2: 1.2.3 backpack/theme-coreuiv4: 1.1.1 backpack/theme-tabler: 1.2.5

jcastroa87 commented 4 months ago

Hello @zeknoss

Can you share with me the code of the column to test on my side.

Cheers.

zeknoss commented 4 months ago

Hey @jcastroa87 , sure thing!

This one is the code I used for the relationship_count column.

        $this->crud->addColumn([
            'name' => 'customers',
            'type' => 'relationship_count',
            'label' => 'Customers',
            'suffix' => ' Customers',
            'wrapper' => [
                'href' => function ($crud, $column, $entry, $related_key) {
                    return backpack_url('organization/' . $entry->id . '/customer');
                },
            ]
        ]);

When I changed it and the other relationship_count columns into hard coded attributes that gives the count by itself, thing sorted out by itself.

In fact, I can't figure out how to get the error details on the Backpack ajax requests. If you could point me in the direction, I could debug it and give you a better detail.

Setting the APP_DEBUG to true didn't help. Maybe that's because of the production environment. But all my code except backpack successfully reports all the errors to my Sentry. I'd really appreciate your input on this.

jcastroa87 commented 4 months ago

Hello @zeknoss did you check server logs?

How big is the table of model, bacuse without error detaild and sometimes work and then not work, sound like resource limit could be about execute time or memory limit.

Cheers.

zeknoss commented 4 months ago

That's my problem @jcastroa87, I cannot make Backpack routes to report bugs at all! If I could, I would have solved it or posted the details here in the first place. Does anything come to your mind regarding why backpack routes wouldn't trigger Sentry reporting?

pxpm commented 4 months ago

Hello @zeknoss sorry for the trouble. šŸ™

relationship_count is not an optimized column.

It works for some items, but for large quantities is not the best column as it slows down your page significantly.

My advice would be to eager load the relation count in the query, and display that as a text column:

protected function setupListOperation()
{
    CRUD::addClause(fn($query) => $query->withCount('posts')); // assuming "posts" is the name of your relation

    CRUD::addColumn('posts_count');
}

This way the count is done in the database and it's way way faster.

Let me know if that helps.

Cheers

zeknoss commented 4 months ago

Hey @pxpm, Thank you for the input! If this is your recommendation, then I solved it the right way.

Wish you a great day!