Answer the question
In order to leave comments, you need to log in
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;
}
...
Route::group(['prefix' => 'user'], function () {
Route::get('{user}', '[email protected]')->name('user_profile');
Route::post('{user}/info', '[email protected]')->name('user_info');
...
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
No query results for model [App\Models\User].
"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)
// сломанные роуты
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');
http://mysite/user/1
http://mysite/user/1/info
// --- когда так, все работает
http://mysite/1
http://mysite/user/1/info
Answer the question
In order to leave comments, you need to log in
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 () {
....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question