ramzi trabelsi
ramzi trabelsi

Reputation: 852

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

Upvotes: 5

Views: 16277

Answers (5)

saad
saad

Reputation: 51

In Laravel 8.x you can solve this problem with:

Route::group(['middleware' => 'auth','middleware' => 'role'], function () {
  ..........
});

Upvotes: 0

MrMedicine
MrMedicine

Reputation: 63

Simply reverse the order:

 Route::middleware('scope:clock-in')->resource('clock', 'ClockController');

As lagbox stated:

Route::resource() does not return anything.

However middleware does.

Upvotes: 2

Rajat Masih
Rajat Masih

Reputation: 583

Use this

Route::prefix("/dashboard")->middleware(['first','second'])->group(function(){
});

Upvotes: 8

lagbox
lagbox

Reputation: 50501

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.

Upvotes: 7

Jason
Jason

Reputation: 3040

Most likely your resource controller is not resolving to an actual controller. Some things to check

  • Check you actually have an adminController, and that the class name is in the correct case
  • Check that your controller is in the default namespace and, if not, change the controller namespace, or add a namespace attribute to your route
  • Check that your controller is not causing an exception on start that is being ignored resulting in you having a null controller.

Upvotes: 0

Related Questions