'Laravel 8: Array to string conversion while calling route:list

I have a resource controller which is ArticleController and I want to call this controller in web.php, so I coded:

use App\Http\Controllers\Admin\PanelController;
use App\Http\Controllers\Admin\ArticleController;

Route::namespace('Admin')->prefix('admin')->group(function(){
  Route::get('/admin/panel', [PanelController::class, 'index']);
  Route::resource('articles', [ArticleController::class]);
});

Then I tried the command php artisan route:list to check the routes but I get this error message:

ErrorException

Array to string conversion

So why this error occurs, how can I fix it?



Solution 1:[1]

Route::resource is expecting a string for the second argument, not an array.

Route::resource('articles', ArticleController::class);

Remove the call to namespace for the group, you don't need any namespace prefix because you are using the Fully Qualified Class Name, FQCN, to reference the Controllers.

Route::prefix('admin')->group(function () {
    Route::get('/admin/panel', [PanelController::class, 'index']);
    Route::resource('articles', ArticleController::class);
});

Solution 2:[2]

This is happening because you kept the ArticleController in a square bracket. Remove the square bracket and Leave it like this:

use App\Http\Controllers\Admin\PanelController;
use App\Http\Controllers\Admin\ArticleController;

Route::namespace('Admin')->prefix('admin')->group(function(){
  Route::get('/admin/panel', [PanelController::class, 'index']);
  Route::resource('articles', ArticleController::class);
});

Or

use App\Http\Controllers\Admin\PanelController;

Route::namespace('Admin')->prefix('admin')->group(function(){
  Route::get('/admin/panel', [PanelController::class, 'index']);
  Route::resource('articles', 'App\Http\Controllers\Admin\ArticleController');
});

The resource method is expecting a reference to the controller class as second parameter but you gave it an array.

Note if you are using the first snippet, make sure the class is correctly imported.Else you will run into another error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 yahuza mushe abdul hakim