1

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?

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

2 Answers2

0

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.
    }
Dehost
  • 85
  • 1
  • 1
  • 10
0

I don't know if this can be of any help to anyone with a similar problem, but I had everything set up correctly and still didn't work.

My problem was that for some reason the grouped routes didn't use the "binding" middleware. When I added the "binding" middleware to my group is started working again. Bear in mind that I had that set up correctly in my Kernel.php but for some reason it wasn't used.

Leaving this here in case it happens to someone else

truffolone
  • 634
  • 7
  • 7