0

I'm trying to make an authentication login with laravel Sanctum and get create the token using SQL Server and I have the following error:

SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Converting an nvarchar data type to a datetime data type resulted in an out-of-range value. (Connection: sqlsrv, SQL: insert into [personal_access_tokens] ([name], [token], [abilities], [expires_at], [tokenable_id], [tokenable_type], [updated_at], [created_at]) values (insomnia, e1984b68e31dfba8268194d6f7190c65e2702904c4759084634086f970cc9830, ["*"], ?, 1, App\Models\User, 2024-09-24 12:12:03.047, 2024-09-24 12:12:03.047))

If I insert using a query, it works normally, passing the values ​​like this

insert into [personal_access_tokens] ([name], [token], [abilities], [expires_at], [tokenable_id], [tokenable_type], [updated_at], [created_at]) values ('insominia', 'd6f25421129a1ad0d05a82d83ded92859d4209272981f46c3b5377503dbfd312', '["*"]', CONVERT(datetime, '2050-02-23 06:20:24.000', 121), 1, 'App\Models\User', CONVERT(datetime, '2016-02-23 06:20:24.000', 121), CONVERT(datetime, '2016-02-23 06:20:24.000', 121))

Or using DB::raw:

DB::raw(insert into [personal_access_tokens] ([name], [token], [abilities], [expires_at], [tokenable_id], [tokenable_type], [updated_at], [created_at]) values ('insominia', 'd6f25421129a1ad0d05a82d83ded92859d4209272981f46c3b5377503dbfd312', '["*"]', CONVERT(datetime, '2050-02-23 06:20:24.000', 121), 1, 'App\Models\User', CONVERT(datetime, '2016-02-23 06:20:24.000', 121), CONVERT(datetime, '2016-02-23 06:20:24.000', 121)))

I tested the createToken function in the HasApiToken.php file in the vendor/sanctum folder.

My Laravel login function:

public function login(Request $request)
{
    $user = User::where('email', $request->email)->first();
    $token = $user->createToken($request->device_name)->plainTextToken;
    return response()->json([
        'token' => $token,
        'user' => $user
    ]);
}
8
  • 1
    set language 'spanish'; select cast('2050-02-23 06:20:24.000' as datetime) fails for me. You need to use non-ambigious date string Commented Sep 24, 2024 at 12:42
  • 1
    This question is similar to: SQL Server not accepting YYYY-MM-DD. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 24, 2024 at 12:43
  • I can do inserts using a query in SQL Server, my problem is creating an authentication token through Laravel 10. If I try to use Insomnia, for example, to try to log in, I get the error above. Commented Sep 24, 2024 at 12:47
  • I think the ?, in the query is throwing things off. Can you show the code you're using? Commented Sep 24, 2024 at 12:49
  • Sounds like it doesn't properly support spanish date format, i'd say it's a bug. Commented Sep 24, 2024 at 12:51

0

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.