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

12,043

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);
});
Share:
12,043
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Admin
    Admin over 3 years
    I removed the array around the Controller, but now it says: Target class [Admin\App\Http\Controllers\Admin\ArticleController] does not exist.
  • BABAK ASHRAFI
    BABAK ASHRAFI over 3 years
    Because you're adding a namespace before any controller being defined in the route file. You can check namespace in the RouteServiceProvider or the group of route that you've defined
  • G3rr0n
    G3rr0n almost 3 years
    @ruwroveajaic , i had the same issue recently, this can be fixed by putting the entire class path in a string like Route::resource('articles', 'App\Http\Controllers\ArticleController'); assuming your controller is in a similar path, so you change the path accordingly.