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

mariohbrino's avatar

Spark 7.0 error with Route Model Binding

I have one issue with Laravel Spark 7.0, the route model binding doesn't abort when it passes a string. It works fine at Laravel 5.7, only in Spark it happens.

Any one can help me with this? Thanks.

Route

Route::get('api/users/{user}', function (App\User $user) {
    return $user->email;
});

Example: app.local/api/user/test

Result

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "test" (SQL: select * from "users" where "id" = test limit 1)
0 likes
4 replies
mariohbrino's avatar

@Cronix

Thanks, I forget to say that, I saw that before. But, I don't want to do it for each route I have, in case I forget it will be bad.

There is a case when I'll use resource, and this function where is not option for resource routes.

Route

Route::resource('/api/user', 'UserController');

Controller

public function show(User $user) {
    return $user;
}
Cronix's avatar
Cronix
Best Answer
Level 67

If you scroll down to the next section in the docs, you can set them globally so it's not tied to a specific route.

public function boot()
{
    Route::pattern('user', '[0-9]+');

    parent::boot();
}
mariohbrino's avatar

@Cronix

Thank you.

That worked and is what I needed in the begin. I was thinking and looking for a way to make it generic as well, so I do not have to create a new one for each model. Do you have one idea how to apply to all models?

Thanks.

Please or to participate in this conversation.