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

BitValentine's avatar

Using Sanctum with Laravel Spark conflict

My setup

  • Laravel 8
  • Laravel Spark Mollie

I'm constantly hitting a brick wall when calling API requests with Spark & Sanctum. I've installed Sanctum with no problem and migrated.

I've added use Laravel\Sanctum\HasApiTokens; to app/Models/User.php and added use HasApiTokens; to the class.

My Api.php route

Route::group([
    'middleware' => 'auth:sanctum'
], function () {
    Route::get('categories', [\App\Http\Controllers\categories::class, 'fetchCategories']);
});

When I call the Api I get this error

ErrorException
Declaration of Laravel\Sanctum\HasApiTokens::tokenCan(string $ability) should be compatible with Laravel\Spark\User::tokenCan($ability)

I've tried changing use Laravel\Sanctum\HasApiTokens; to Laravel\Spark\HasApiTokens on User.php. The error goes away, but whenever I try calling the Api, it returns me back to the login homepage.

Any ideas? As the Spark documentation doesn't really explain how Sanctum or Api protection work.

0 likes
1 reply
ahoi's avatar

I got the same problem using Passport instead of Sanctum.

In my case it seems to be a problem when the default middleware is active.

    /*
    |--------------------------------------------------------------------------
    | Spark Middleware
    |--------------------------------------------------------------------------
    |
    | These are the middleware that requests to the Spark billing portal must
    | pass through before being accepted. Typically, the default list that
    | is defined below should be suitable for most Laravel applications.
    |
    */

    'middleware' => [], //TODO: Check, which middleware has to be passed

Using this empty array, I don't get redirected, but I have to figure out, how this impacts the security of my app.

Please or to participate in this conversation.