Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

atif-bashir-1998's avatar

A resource controller inside a route group throws an error

I have PHP 8, Laravel 9 with Inertia (Vue 3). I have a resource controller inside a grouped route. See the code below:

Route::group([
  'prefix' => '{locale?}',
  'where' => ['locale' => '[a-zA-Z]{2}'],
  'middleware' => 'set-locale',
], function () {

  Route::resource('/sliders', SliderManagement::class);
});

I created a link in a vue page to visit sliders.show. Check the vue code below:

<a v-for="slider in sliders" :key="slider.id" :href="route('sliders.show', {slider: slider.id, locale: locale})">Go to slider {{ slider.id }}</a>

When I visit the link, I am getting the following error:

App\Http\Controllers\Admin\SliderManagement::show(): Argument #1 ($slider) must be of type App\Models\Slider, string given, called in /home/atif/Desktop/Freelancing/Sebastian Projects/98goal.com/98goal.com/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54

The code related to the show method is as follows:

public function show(Slider $slider) {
    return Inertia::render('Admin/Slider/Show', ['slider' => $slider]);
  }
0 likes
13 replies
Tray2's avatar

This is the problem. Argument #1 ($slider) must be of type App\Models\Slider, string given .

You are passing a string to a method, when it expects a instance of the Slider model class.

atif-bashir-1998's avatar

@Tray2 I have edited the post. Instead of showing the code for the <a> tag, the post rendered it as a link. Please check the code how I am accessing the desired route.

Actually the issue is instead of getting the $slider variable in the show(), laravel is reading the $locale variable. If I add 2 parameters in my show() method, it does not give this error

Tray2's avatar

@atif-bashir-1998 You should always share any code between three back ticks `

public function show()
{}

Never link to some strange site if it can be avoided.

You can link to the project or a gist on GitHub if there is need for it.

kokoshneta's avatar
Level 27

@atif-bashir-1998 Your routes are defined with the locale variable first, the slider variable second. Note that the docs say:

Route parameters are injected into route callbacks / controllers based on their order - the names of the route callback / controller arguments do not matter.

That means when you pass the slider ID first and the locale second, it will interpret the slider ID as the locale string and the locale as the slider ID. Since the slider ID won’t match your regex for locale strings, it is ignored.

I think it should work if you switch the order of the route parameters in the link to {locale: locale, slider: slider.id} (or indeed just [locale, slider.id]?).

atif-bashir-1998's avatar

@kokoshneta I changed the order of the parameters but nothing happened. Then I updated the show function as shown below:

public function show($locale, Slider $slider) {
    return Inertia::render('Admin/Slider/Show');
  }

It worked. But now the issue is that when the Inertia renders the view, I am getting the following ziggy error:

Uncaught (in promise) Error: Ziggy error: 'slider' parameter is required for route 'sliders.show'.
kokoshneta's avatar

@atif-bashir-1998 Unfortunately, I know nothing about Inertia, so I can’t help you with that error.

But why are you rendering the Inertia template for editing in the method for showing? Could that be the problem?

atif-bashir-1998's avatar

@kokoshneta Sorry for the bad naming convention. That was not the problem. I have edited my post and comments to avoid any confusion. Thanks for your help

atif-bashir-1998's avatar

@kokoshneta I just figured out the issue was in another .vue file. I was using a layout vue file which was having this issue. I removed that file and tested. It worked

Tray2's avatar

@atif-bashir-1998 You are telling the method that a Slider should be passed into it. The method expects you to use route model binding, https://laravel.com/docs/9.x/routing#route-model-binding

You can either update your route to support route model binding or you can change the

public function show(Slider $slider) {

To

public function show($slider) {
atif-bashir-1998's avatar

@Tray2 when I use your suggestion, but it did not work. I guess the issue was because I was accepting only 1 route parameter $slider. I updated the show() and this issue was fixed. But then I got another issue.

Please or to participate in this conversation.