Laravel 7路线模型绑定不起作用(路线中的模型始终为空) [英] Laravel 7 Route Model Binding is not working (Model in the route is always null)

查看:41
本文介绍了Laravel 7路线模型绑定不起作用(路线中的模型始终为空)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Laravel 7项目.在我的项目中,我正在进行路线模型绑定.但是它不起作用,并且路由中的模型始终返回null.这是我到目前为止所做的.

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.

我声明一条路线

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

如您所见,模型绑定有一个占位符{category}.

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.
    }

在操作方法中,即使我传递了正确的ID,$ category也始终为null.我的代码有什么问题,我该如何解决?

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?

推荐答案

首先必须订购,控制器方法是首先请求$ request,然后进行模型注入:

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.
    }

这篇关于Laravel 7路线模型绑定不起作用(路线中的模型始终为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆