Liam Seys
Liam Seys

Reputation: 31

Illuminate\Foundation\Auth\User returning keyType: int

I am using the following trait to generate UUID's for my user model. I have setup a listener to the following event: Illuminate\Auth\Events\Login. This works when you just normally sign-in to the app (using Laravel Breeze). But when I manually do auth()->user($user) I get back an instance of Illuminate\Foundation\Auth\User where the keyType is an integer and incrementing is set to true, although my trait does otherwise.

Anyone knows how to fix this?

<?php

namespace App\Traits;

use Ramsey\Uuid\Uuid;

trait Uuidable
{
    /**
     * Cast the primary key type as a string
     */
    public function initializeUuidable()
    {
        $this->setKeyType('string');
    }

    /**
     * Set the model to none incrementing
     *
     * @return bool
     */
    public function getIncrementing()
    {
        return false;
    }

    /**
     * Set the key of the model
     *
     * @return void
     */
    public static function bootUuidable()
    {
        static::creating(function ($model) {
            if (!isset($model->attributes[$model->getKeyName()])) {
                $model->incrementing = false;
                $uuid = Uuid::uuid4();
                $model->attributes[$model->getKeyName()] = $uuid->toString();
            }
        }, 0);
    }
}

Illuminate\Foundation\Auth\User {#1494 ▼
  #connection: "mysql"
  #table: "users"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:13 [▶
    "id" => "31f12e1a-964c-4580-8a20-263c8bb7bf95"
    "first_name" => "test"
    "last_name" => "user"
    "email" => "[email protected]"
    "email_verified_at" => null
    "password" => "$2y$10$vTAYxvC4s95PpF5BmzC0p..ZiQfcn3WARxXalgmd1kVZn5X1S/F02"
    "last_login_id" => null
    "remember_token" => null
    "welcome_valid_until" => null
    "printer_id" => null
    "created_at" => "2021-09-15 14:38:00"
    "updated_at" => "2021-09-15 14:38:20"
    "deleted_at" => null
  ]
  #original: array:13 [▶
    "id" => "31f12e1a-964c-4580-8a20-263c8bb7bf95"
    "first_name" => "test"
    "last_name" => "user"
    "email" => "[email protected]"
    "email_verified_at" => null
    "password" => "$2y$10$vTAYxvC4s95PpF5BmzC0p..ZiQfcn3WARxXalgmd1kVZn5X1S/F02"
    "last_login_id" => null
    "remember_token" => null
    "welcome_valid_until" => null
    "printer_id" => null
    "created_at" => "2021-09-15 14:38:00"
    "updated_at" => "2021-09-15 14:38:20"
    "deleted_at" => null
  ]
  #changes: array:3 [▶
    "password" => "$2y$10$vTAYxvC4s95PpF5BmzC0p..ZiQfcn3WARxXalgmd1kVZn5X1S/F02"
    "welcome_valid_until" => null
    "updated_at" => "2021-09-15 14:38:20"
  ]
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶
    0 => "*"
  ]
  #rememberTokenName: "remember_token"
}

Upvotes: 1

Views: 189

Answers (0)

Related Questions