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

AR's avatar
Level 7

SQLSTATE[HY000]: General error: 2031

I am testing if a user tries to enter an invalid url for editing a product. The products.edit route has two parameters. Shop url and product hashedid. Basically it looks like this:

MybestShop.app/emory58/products/Oaa61xK1gbQk/edit

In my test I am testing this in two steps. The first test is that I want to get a 404 when the shop name (emory58) is not correct. In the second test I am testing if the product's hashed id (Oaa61xK1gbQk) is not correct. In My test I get both tests passing and they return 404. But when I do the same thing in the browser entering an invalid shop gives me the 404 page but the invalid hashed id gives me the error which is pasted at the bottom of this post.

Here is my test that passes with no issues:

/** @test */
public function edit_products_page_url_must_be_valid()
{
    $this->response = $this->withExceptionHandling()->actingAs($this->user)
        ->get(route('products.edit', ['invalid-shop-adress', $this->product->hashedid]));

    $this->response->assertStatus(404);

    $this->response = $this->withExceptionHandling()->actingAs($this->user)
        ->get(route('products.edit', [$this->shop->url, 'invalid-hash-id']));

    $this->response->assertStatus(404);
}

Here is my Route:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/{shop}/products/{hashedid}/edit', 'ProductsController@edit')->name('products.edit');
});

Here is my Controller's edit method:

public function edit($shop, $hashedid)
    {
        $product = Product::findByHashedId($hashedid);

        $shop = Shop::findByUrl($shop);

        $this->authorize('update', [$product, $shop]);

        return view('products.edit', compact('product', 'shop'));
    }

Here is the Product's methods involved:

public $hashids;

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    // Hashids is set up in the AppServiceProvider
    $this->hashids = app('Hashids\Hashids');
}
public static function findByHashedId($hashedid)
{
    return self::where('id', (new Product)->hashids->decode($hashedid))->firstOrFail();
}

Errors:

1/2
PDOException in Connection.php line 323:
SQLSTATE[HY000]: General error: 2031
in Connection.php line 323
at PDOStatement->execute() in Connection.php line 323
at Connection->Illuminate\Database\{closure}('select * from `products` where `id` = ? limit 1', array()) in Connection.php line 640
at Connection->runQueryCallback('select * from `products` where `id` = ? limit 1', array(), object(Closure)) in Connection.php line 607
at Connection->run('select * from `products` where `id` = ? limit 1', array(), object(Closure)) in Connection.php line 326
at Connection->select('select * from `products` where `id` = ? limit 1', array(), true) in Builder.php line 1711
at Builder->runSelect() in Builder.php line 1696
at Builder->get(array('*')) in Builder.php line 463
at Builder->getModels(array('*')) in Builder.php line 447
at Builder->get(array('*')) in BuildsQueries.php line 71
at Builder->first(array('*')) in Builder.php line 392
at Builder->firstOrFail() in Product.php line 43
at Product::findByHashedId('Oaa61xK1gbQk') in ProductsController.php line 80
at ProductsController->edit('emory58', 'Oaa61xK1gbQk')
at call_user_func_array(array(object(ProductsController), 'edit'), array('shop' => 'emory58', 'hashedid' => 'Oaa61xK1gbQk')) in Controller.php line 55
at Controller->callAction('edit', array('shop' => 'emory58', 'hashedid' => 'Oaa61xK1gbQk')) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(ProductsController), 'edit') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Language.php line 27
at Language->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
2/2
QueryException in Connection.php line 647:
SQLSTATE[HY000]: General error: 2031 (SQL: select * from `products` where `id` = ? limit 1)
in Connection.php line 647
at Connection->runQueryCallback('select * from `products` where `id` = ? limit 1', array(), object(Closure)) in Connection.php line 607
at Connection->run('select * from `products` where `id` = ? limit 1', array(), object(Closure)) in Connection.php line 326
at Connection->select('select * from `products` where `id` = ? limit 1', array(), true) in Builder.php line 1711
at Builder->runSelect() in Builder.php line 1696
at Builder->get(array('*')) in Builder.php line 463
at Builder->getModels(array('*')) in Builder.php line 447
at Builder->get(array('*')) in BuildsQueries.php line 71
at Builder->first(array('*')) in Builder.php line 392
at Builder->firstOrFail() in Product.php line 43
at Product::findByHashedId('Oaa61xK1gbQk') in ProductsController.php line 80
at ProductsController->edit('emory58', 'Oaa61xK1gbQk')
at call_user_func_array(array(object(ProductsController), 'edit'), array('shop' => 'emory58', 'hashedid' => 'Oaa61xK1gbQk')) in Controller.php line 55
at Controller->callAction('edit', array('shop' => 'emory58', 'hashedid' => 'Oaa61xK1gbQk')) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(ProductsController), 'edit') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Language.php line 27
at Language->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Debugbar.php line 51
at Debugbar->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53

Please advice

0 likes
3 replies
shez1983's avatar

are you forgetting to set an app env for hasids to work properly perhaps in you .env? i remember vaguely that you need one..

can you verify what this outputs by doing a dump: (new Product)->hashids->decode($hashedid))

can you remove that with a hardcoded product id to see if that works? because from the logs issue definitely seems to be around here somewhere..

AR's avatar
Level 7

@shez1983 Thanks for the reply!

There is no issues with the hashids as it works fine. The reason I am sure is that when the info is correct I get the results just fine. My issue is only when the hashid is not a valid id which does not give a 404 in the browser (but gives 404 in the test!).

I have set up the things Hashids needs in the AppServiceProvider and also the hashids functionality works in my other tests like "a product can be found by hashid". Even the test I have shown to you is working fine and returns the 404. The problem is only when I manually enter an invalid hashid.

Now when I dump the part that you told me, here is the result that gets dumped:

// When the hashid passed is correct, dd() gives me a value of the id  for example "62"
dd((new Product)->hashids->decode($hashedid)));

// When the hashid passed is not valid, dd() gives me empty array []
dd((new Product)->hashids->decode($hashedid)));

I still don't know why the test passes and returns the 404 but the browser gives me this error.

Please advice.

AR's avatar
AR
OP
Best Answer
Level 7

The problem was that when the hashid was invalid it was converted to [] and in my testing it was passing fine because my database was sqlite but it was failing in the browser which I am using mysql database for. I changed the code so that if there is no value for the hashid that is converted then null will be passed to the query.

public static function findByHashedId($hashedid)
    {
        return self::where(
            'id', (new Product)->hashids->decode($hashedid) ?: null
            )->firstOrFail();
    }

Please or to participate in this conversation.