Search code examples
phpsql-serverlaraveldate-formattinglaravel-passport

Change Laravel Passport oauth_access_token.expires_at dateformat


I work with a sql server so I have to change the dateformat in Laravel Passport classes like this :

 <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Passport\HasApiTokens;
use Laravel\Passport\Passport;
use Laravel\Passport\Token as PassportToken;


class AccessToken extends PassportToken
{
    use HasApiTokens;

    protected $dateFormat = 'Y-d-m H:i:s.v';

}

But that do not affect the expires_at column, so I get this error when I try t create a new access_token:

Illuminate\Database\QueryException: SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]La conversion d&#039;un type de données nvarchar en type de données datetime a créé une valeur hors limites. (SQL: insert into [oauth_access_tokens] ([id], [user_id], [client_id], [scopes], [revoked], [created_at], [updated_at], [expires_at]) values (881cbbfc54a224d25bd1d657c7b681ef0c93814e3ac599a832e7136823ce385dffeaf89d5f971668, ?, 96FACD5E-4B2A-44C6-9C48-EEB36680C559, [], 0, 2022-08-16 08:47:56.027, 2022-08-16 08:47:56.027, 2022-16-09 08:47:56.027)) in file C:\Git\metarom-hub-services\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 759

My sql server is set up in french so sorry for the french error.

As you can see, the created_at and updated_at have the same format as I told in my model class but not the expires_at.

How can I do to change that ?


Solution

  • I add in my Models:

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'created_at',
        'updated_at',
        'expires_at',
    ];
    

    That I found in the Passports Models and it works. Looks like I have to tell explicitely that the 'expires_at' is also a date so It has to take the same format.