0

I've just created a new laravel project and setup the linting automation, but a lint error popped up in the return statement.

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

Why the lint error pops up?

4
  • 1
    $request->user()?->id Why you think this is valid PHP? Commented Apr 3, 2022 at 8:50
  • 1
    @LarsStegelitz Because as of PHP 8, it is. Commented Apr 3, 2022 at 11:15
  • 1
    Please check your PHP version. The nullsafe operator you're using was introduced in PHP 8. It is not supported on PHP 7. Commented Apr 3, 2022 at 11:16
  • 1
    If you are using PHP 8 and it's just a lint error, your linter might think you're using PHP 7 so you'll want to check its settings. Commented Apr 3, 2022 at 11:34

4 Answers 4

1

Check your PHP and Laravel versions. Laravel 9 requires PHP8. https://laravel.com/docs/9.x/releases

PHP 8 supports nullsafe operator https://kinsta.com/blog/php-8/#nullsafe-operator

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is in your code. Use the following code :

    protected function configureRateLimiting(){

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

Comments

0

This my solve you issue. Rewrite the ternary operator as below

   protected function configureRateLimiting(){

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

Comments

0

I changed this and issue resolved

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.