Laravel 8: Array to String conversion on route register

Viewed 5498

Is there any way to define the name of the route group in laravel 8?

I'm trying to build routes for the sellers to go to the order management site, and below is my routes list in web.php

use App\Http\Controllers\Seller\OrderController;

Route::group(['prefix' => 'seller', 'middleware' => 'auth', 'as' => 'seller.', 'namespace' => 'Seller'], function () {

    Route::redirect('/','seller/orders');

    Route::resource('/orders',  [OrderController::class]);
});

Errors

ErrorException 

Array to string conversion

at C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
412▕     protected function getResourceAction($resource, $controller, $method, $options)
413▕     {
414▕         $name = $this->getResourceRouteName($resource, $method, $options);
415▕
➜ 416▕         $action = ['as' => $name, 'uses' => $controller.'@'.$method];
417▕
418▕         if (isset($options['middleware'])) {
419▕             $action['middleware'] = $options['middleware'];
420▕         }

1   C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
    Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php", ["orders", "index", "orders.index"])

2   C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:189
    Illuminate\Routing\ResourceRegistrar::getResourceAction("orders", "index", [])

3 Answers

Change the following line

Route::resource('/orders',  [OrderController::class]);

to

Route::resource('/orders',  OrderController::class);

The reason it is giving you that error is because you are passing that `OrderController as an array:

Route::resource('/orders',  [OrderController::class]);

You are probably confusing the syntax for regular routes with the syntax for resource controllers.

For resource controllers you have to pass the class name as a string:

Route::resource('/orders',  OrderController::class);

this worked for me. SO @Iyathurai Iyngaran

Change the following line

use locationcontroller/OrderController as OrderController;

Route::resource('/orders',  OrderController::class);
Related