Search code examples
laravelroutesmiddlewarelaravel-5.4

Call to a member function middleware() on null


I use the following middleware in routing Laravel:

Route::group(['prefix' => 'api/'], function() {
    Route::resource('admin', 'adminController')->middleware('auth');
    Route::resource('profile', 'profileController')->middleware('role');
}); 

I get this error when i call 'admin' or 'profile' path in URL enter image description here


Solution

  • It is because Route::resource() does not return anything. Its void. It doesn't return an object.

    Laravel 5.4 - Illuminate\Routing\Router@resource

    In Laravel 5.5 (in development), Route::resource() will be returning an object for fluently adding options to.