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
]);
}
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?,in the query is throwing things off. Can you show the code you're using?