Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ludo1960's avatar

How to debug a query

Hi guys,

got a rather annoying issue, there seems to be something wrong with one of my queries, to try and remedy this I installed https://github.com/barryvdh/laravel-debugbar hoping that it would help me find out which query is at fault. I've tried following a few examples I found on the internet, but can't find anything specific for queries. The error I get is "bad sort specification" and a file name (it's mongodb and query works fine in MySQL) I know where the queries are (or at least I think I do)

How do I go about targeting the query at fault?.

0 likes
2 replies
tokoiwesley's avatar

What database are you using? Also, it will be helpful to share the error message in detail - as it appears.

ludo1960's avatar

Hello,

I'm using mongoDB, and the error is a rather unhelpful

bad sort specification
/var/www/slimmongo.local.com/vendor/mongodb/mongodb/src/Operation/Find.php#299

After more digging I fount that the error is triggered by a function in a blade file:

 {{-- @each('admin::partials.menu', Admin::menu(), 'item') --}}

if I uncomment this the site crashes

Looking the Admin Model I find it uses pivot tables, great for a relational DB but not so great for Mongo:

    /**
     * A user has and belongs to many roles.
     *
     * @return BelongsToMany
     */
    public function roles() : BelongsToMany
    {
        $pivotTable = config('admin.database.role_users_table');

        $relatedModel = config('admin.database.roles_model');

        return $this->belongsToMany($relatedModel, $pivotTable, 'user_id', 'role_id');
        //return $this->belongsToMany($relatedModel, null, 'user_id', 'role_id');
    }

According to this https://github.com/jenssegers/laravel-mongodb#relations, I need to adjust the queries as you see in the // code above. But i'm getting nowhere fast!

I was hoping the debugger would give me more to go on, I tried adding this to the blade file:

           @php

                try {
                    throw new Exception( Admin::menu() );
                    } catch (Exception $e) {
                    Debugbar::addException($e);
                }

            @endphp

but it just gives me the same error as above. Dunno what to do next?

Please or to participate in this conversation.