Skip to content

Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given #2264

Open
@farshan-dev

Description

@farshan-dev

in laravel 11.x

This problem occurs when JWT_LEEWAY is set to 1 in the .env file.

Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given, called in C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Units.php on line 356 {"exception":"[object] (TypeError(code: 0): Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given, called in C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Units.php on line 356 at C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Units.php:455)
[stacktrace]
#0 C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Units.php(356): Carbon\Carbon::rawAddUnit(Object(Carbon\Carbon), 'second', '1')
#1 C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Date.php(2925): Carbon\Carbon->addUnit('second', '1', NULL)
#2 C:\codes\hst\vendor\nesbot\carbon\src\Carbon\Traits\Date.php(2616): Carbon\Carbon->callModifierMethod('second', Array)
#3 C:\codes\hst\vendor\tymon\jwt-auth\src\Support\Utils.php(51): Carbon\Carbon->__call('addSeconds', Array)
#4 C:\codes\hst\vendor\tymon\jwt-auth\src\Claims\DatetimeTrait.php(80): Tymon\JWTAuth\Support\Utils::isPast(Object(Carbon\Carbon), '1')
#5 C:\codes\hst\vendor\tymon\jwt-auth\src\Claims\Expiration.php(30): Tymon\JWTAuth\Claims\Expiration->isPast(1717051371)
#6 [internal function]: Tymon\JWTAuth\Claims\Expiration->validatePayload()

Activity

DouglasAR01

DouglasAR01 commented on Jul 20, 2024

@DouglasAR01

I solved it (and other similar issues) by adding the int cast (int) to every "time-based" config that may come from the .env file. For example:
'ttl' => (int) env('JWT_TTL', 60)
'refresh_ttl' => (int) env('JWT_REFRESH_TTL', 20160)

ubaid332

ubaid332 commented on Aug 5, 2024

@ubaid332

I solved it (and other similar issues) by adding the int cast (int) to every "time-based" config that may come from the .env file. For example: 'ttl' => (int) env('JWT_TTL', 60) 'refresh_ttl' => (int) env('JWT_REFRESH_TTL', 20160)

Thank my issue solved as well by using (int) in jwt.php

davemorris80

davemorris80 commented on Dec 12, 2024

@davemorris80

Bravo! This fixed my issue too after upgrading to Laravel 11. I'm using JWT for API authentication and just needed to add (int) in the config/jwt.php file to:

  • 'ttl' => (int) env('JWT_TTL', 60),
    
  • 'refresh_ttl' => (int) env('JWT_REFRESH_TTL', 20160),
    
  • 'leeway' => (int) env('JWT_LEEWAY', 0),
    
  • 'blacklist_grace_period' => (int) env('JWT_BLACKLIST_GRACE_PERIOD', 0),
    
specialtactics

specialtactics commented on Dec 30, 2024

@specialtactics

I wonder how we can improve this situation in the package.

We can change the default config file that's publish to include those casts, however it won't have any affect on existing installations, and looks a bit messy IMO.

We can add some code to internally cast all integer configs to int after reading the config, this will have an immediate impact on existing users, without them having to change anything (other than package update of course), however then we run the risk of forgetting to do that for any new int configs. This risk can be mitigated by adding more typing to the project - for example, leeway can be typed to int across the codebase, as can ttls and so forth. I guess with current trends, this is something we should do anyway.

Keen to hear other's thoughts.

miguelquesadamartinez

miguelquesadamartinez commented on Mar 3, 2025

@miguelquesadamartinez

Have migrated to Laravel 12 now with sanctum, snd returned me the same error, and resolved the same way
return $user->createToken($request->device_name, ['*'], now()->addMinutes( (int) env('SANCTUM_TOKEN_EXPIRATION')))->plainTextToken;
Just add (int) before the env() function

haykhov

haykhov commented on Mar 6, 2025

@haykhov

The error occurs because Carbon’s rawAddUnit() function expects the third argument to be an int or float, but a string was given. This usually happens when you’re trying to add a value, but it’s passed as a string. To fix this, you need to convert the string to a numeric type.

$date->addDay('1'); // incorrect
$date->addDay(1); // correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @specialtactics@haykhov@davemorris80@ubaid332@DouglasAR01

        Issue actions

          Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given · Issue #2264 · tymondesigns/jwt-auth