B
B
BonBon Slick2017-06-14 17:22:21
Laravel
BonBon Slick, 2017-06-14 17:22:21

Laravel routing/response, No query results for Model User?

In the docks, I found how to do it right, out of the box, redirects to new model binding fields. Automatically, when we pass the model to the url, laravel searches by the model ID. In the docks, it is written that you need to do this , to rewrite the selection in another field:

/**
     * Use a database column other than default ID in URL for model biding: /{user}/... == /{id}/
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }

    /**
     * if you would like to customize the value that is placed in the route parameter  (route('profile', [$user]) == profile/{id})
     */
    public function getRouteKey()
    {
        return $this->slug;
    }

The first function is for url and the second is for route. More about them in the docs.
1 - https://laravel.com/docs/5.4/routing#route-model-b...
2 - https://laravel.com/docs/5.4/responses#redirecting...
Now all routes of this type are broken :
...
 Route::group(['prefix' => 'user'], function () {
            Route::get('{user}', '[email protected]')->name('user_profile');
            Route::post('{user}/info', '[email protected]')->name('user_info');
...

ERROR:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

No query results for model [App\Models\User].

I tried to change the overwrite to ID, replaced $this->slug with $this->id and "id", now this error:
"SQLSTATE[22P02]: Invalid text representation: 7 ERROR:  invalid input syntax for integer: "info"
 (SQL: select * from "users" where "id" = info and "users"."deleted_at" is null limit 1)

As you can see, it took the info url and stuck it in a selection. Same if you clear the rewrite functions, even after composer dump-autoload + php artisan cache:clear + route:clear . Now it sculpts in SQL yurl.
That is, now I can’t transfer the model through binding even by default.
Cleared the cache. Perhaps this is a facade of some kind, or a middleware.
To find out the trigger where the request is called ...
After a couple of trials, I noticed the following.
// сломанные роуты
 Route::group(['prefix' => 'user'], function () {
            Route::get('{user}', '[email protected]')->name('user_profile');
            Route::post('{user}/info', '[email protected]')->name('user_info');
// рабочие
  Route::get('{user}', '[email protected]')->name('user_profile');
 Route::group(['prefix' => 'user'], function () {
            Route::post('{user}/info', '[email protected]')->name('user_info');

There are ideas what could cause this behavior, I think it's not really about rewriting the url model binding .
The url that breaks the rest of mysite/user/1 :
http://mysite/user/1
http://mysite/user/1/info 
// --- когда так, все работает
http://mysite/1
http://mysite/user/1/info

What did I miss, why is the model not loading for me?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2017-06-14
@BonBonSlick

It seems strange, everything was in order ...

// USER PROFILE
        Route::group(['prefix' => 'user'], function () {
...
            Route::post('{user}/info', '[email protected]')->name('user_more_info');
            Route::get('{user}', '[email protected]')->name('user_profile');
            // routes that need Blacklist check
            Route::group(['middleware' => ['blacklisted']], function () {
....

The solution turned out to be not very obvious, although this question is closed, I now wonder why this is so? Who knows, tell me please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question