wzee1

PersonalAccessToken.php

Jul 23rd, 2025
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Models;
  4.  
  5. use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
  6. use MongoDB\Laravel\Eloquent\DocumentModel;
  7. use MongoDB\BSON\ObjectID;
  8.  
  9. class PersonalAccessToken extends SanctumPersonalAccessToken
  10. {
  11.     use DocumentModel;
  12.  
  13.     protected $connection = 'mongodb';
  14.     protected $collection = 'personal_access_tokens';
  15.  
  16.  
  17.     protected $primaryKey = '_id';
  18.     protected $keyType = 'string';
  19.     public $incrementing = false;
  20.  
  21.  
  22.  
  23.     protected $fillable = [
  24.         'name',
  25.         'token',
  26.         'abilities',
  27.         'last_used_at',
  28.         'expires_at',
  29.         'session_id'
  30.     ];
  31.  
  32.     /**
  33.      * Set the tokenable_id attribute, ensuring it's a MongoDB ObjectID.
  34.      * This handles cases where a string ID might be passed.
  35.      *
  36.      * @param  mixed  $value
  37.      * @return void
  38.      */
  39.     public function setTokenableIdAttribute($value)
  40.     {
  41.         // If the value is already an ObjectID, use it directly.
  42.         // Otherwise, attempt to create an ObjectID from the string.
  43.         $this->attributes['tokenable_id'] = (string) $value;
  44.     }
  45.  
  46.     /**
  47.      * Cast the tokenable_id attribute to ObjectID when retrieving.
  48.      * While not strictly necessary for saving, it ensures consistency.
  49.      *
  50.      * @var array
  51.      */
  52.     protected $casts = [
  53.         'abilities' => 'array',
  54.         'expires_at' => 'datetime',
  55.         'last_used_at' => 'datetime',
  56.     ];
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment