Home > other >  unexpected '->' (T_OBJECT_OPERATOR) laravel
unexpected '->' (T_OBJECT_OPERATOR) laravel

Time:04-03

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?

CodePudding user response:

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

CodePudding user response:

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());
    });
}

CodePudding user response:

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());
   });
}
  • Related