Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jadenadamsq's avatar

Laravel Sanctum returns 500 when trying to access sanctum protected API

I am using Laravel 8.12 with PostgreSQL. I am trying to use Laravel Sanctum as authentication for my API. This is the schema for sanctum:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePersonalAccessTokensTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('personal_access_tokens', function (Blueprint $table) {
            $table->uuid('id')->primary()->default(new Expression("uuid_generate_v4()"));
            $table->uuidMorphs('tokenable');
            $table->string('name');
            $table->string('token', 64)->unique();
            $table->text('abilities')->nullable();
            $table->timestamp('last_used_at')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('personal_access_tokens');
    }
}

And this is my user table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary()->default(new Expression('uuid_generate_v4()'));
            $table->string('first_name', 255);
            $table->string('last_name', 255);
            $table->string('email', 255)->index()->unique();
            $table->string('password', 100);
            $table->boolean('email_verified')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Now, when I try to send the token in headers, I get this error: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type uuid: \"0\" (SQL: select * from \"personal_access_tokens\" where \"personal_access_tokens\".\"id\" = 0 limit 1). As from my viewpoint, it looks as if this is the error with column type being uuid?

This is the command I use to make request:

curl --request GET \
  --url http://127.0.0.1:3001/api/user \
  --header 'Accept: application/json' \
  --header 'Authorization: Bearer 0|s0Vpci4xXoEJh1HMEihNd65GLvDmINILaiJr8o8e' \
  --header 'Content-type: application/json'

Any help would be appreciated.

0 likes
2 replies
bobbybouwmann's avatar

Yeah, Laravel automatically assumes you use a bigInteger for the id column of the users table. You can use something else like a UUID, but you would need to override a lot of things in Laravel.

Instead, I would recommend you to keep the bigInteger for the id column in your users table and use an additional uuid column that you can use in the rest of your application.

In this case, you're working too much against the framework defaults that it takes a lot more time to rebuild all those tables and refactor parts of the packages that it's better to use a normal ID.

Note that Sanctum is a way to get started easily, this is really an edge case situation in which the package doesn't provide.

jadenadamsq's avatar
jadenadamsq
OP
Best Answer
Level 1

Alright I got a solution:

Create a file with following contents:

<?php

namespace App\Models;

use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;

class PersonalAccessToken extends SanctumPersonalAccessToken
{
    public $incrementing = true;

    protected $primaryKey = "id";
    protected $keyType = "string";
}

I am calling this file PersonalAccessToken. And then on AppServiceProvider, do something like this:

<?php

namespace App\Providers;

use App\Models\PersonalAccessToken;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        Sanctum::ignoreMigrations();
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
    }
}
2 likes

Please or to participate in this conversation.