'Laravel - Non-static method should not be called statically

I have recently started using laravel for work and I boumped into many issues I cannot stil solve. I have looked over many many topics and already anwered questions but none of those have helped me with my issue So, I get this error trying to do 'php artisan serve' "Non-static method Illuminate\Cache\RateLimiter::for() should not be called statically"

So I went up looking at the code, this is the RateLimiter.php code that gives me the error

protected function configureRateLimiting()
   {
       RateLimiter::for('api', function (Request $request) {
           return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
       });
   } 

the error is at the 2nd line of this code, in the RateLimiter:: etc

I get those errors in the CMD

app/Providers/RouteServiceProvider.php:59 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Non-static method Illuminate\Cache\RateLimiter::for() should not be called statically", "myPathToTheProject/app/Providers/RouteServiceProvider.php", [])

app/Providers/RouteServiceProvider.php:38 App\Providers\RouteServiceProvider::configureRateLimiting()

the function is called like this $this->configureRateLimiting();

hoping you can help me, I will give more infos if are needed



Solution 1:[1]

I found out the problem. You need to import the facade, and when you auto-import, it tends to get the wrong package. Look at the imports. If you find:

use Illuminate\Cache\RateLimiter;

You should replace with

use Illuminate\Support\Facades\RateLimiter;

Worked for me!

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 Luis Gomes