Laravel 7 Route Model Binding is not working (Model in the route is always null)

一个人想着一个人 提交于 2021-01-05 06:41:35

问题


I am working on a Laravel 7 project. In my project, I am doing the route model binding. But it is not working and the model in the route is always returning null. This is what I have done so far.

I declare a route

Route::put('restaurant-category/{category}', 'RestaurantCategoryController@update')->name('restaurant-category.update');

As you can see, there is a placeholder for model binding, {category}.

This is my action in the controller.

public function update(RestaurantCategory $category, UpdateRestaurantCategoryRequest $request)
    {
        //here $category is always null even if I passed the valid category id.
    }

In the action method, the $category is always null even if I passed the correct id for it. What is wrong with my code and how can I fix it?


回答1:


First you have to order, the controller method is first Request $request and then the model injection:

public function update(UpdateRestaurantCategoryRequest $request, RestaurantCategory $category)
    {
        //here $category is always null even if I passed the valid category id.
    }


来源:https://stackoverflow.com/questions/62361037/laravel-7-route-model-binding-is-not-working-model-in-the-route-is-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!