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

raihanuddin's avatar

Laratrust 8.5 isAbleTo() method causing Call to a member function setRelation() on null error after upgrading from Laravel 8 to Laravel 10

I recently upgraded my Laravel project from Laravel 8 to Laravel 10 and Laratrust 7 to Laratrust 8.5. After the upgrade, I encountered an issue when using the isAbleTo() method from Laratrust. The error I am getting is

Call to a member function setRelation() on null

This error occurs when I try to check if a user has a specific permission in my controller using the following code:

if (!$request->user()->isAbleTo('dashboard-widget-box-sale-read')) {
    return $this->CommonUtils->sendError(null, __('common.unauthorized_action'), $request->bearerToken());
}

Here is my User Model:

<?php

namespace App\Models;

use App\Traits\Userstamps;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Contracts\LaratrustUser;
use Laratrust\Traits\HasRolesAndPermissions;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements LaratrustUser
{
    use HasApiTokens, HasFactory, HasProfilePhoto, Notifiable, TwoFactorAuthenticatable, Userstamps;
    use HasRolesAndPermissions;
}

Role:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Laratrust\Models\Role as RoleModel;
use OwenIt\Auditing\Contracts\Auditable;
use App\Traits\Userstamps;

class Role extends RoleModel implements Auditable
{
    use HasFactory, Userstamps;
}

Permission

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laratrust\Models\Permission as PermissionModel;
use OwenIt\Auditing\Contracts\Auditable;
use App\Traits\Userstamps;

class Permission extends PermissionModel implements Auditable
{
    use HasFactory, Userstamps;
}

ERROR LOG

  1. User Authentication: I have confirmed that the user is authenticated and the roles and permissions are assigned correctly.

  2. Permissions Check: The role owner has the permission dashboard-widget-box-sale-read.

  3. Cache Clear: I’ve cleared the application cache and configuration cache

  4. Laratrust Configuration: I’ve also ensured that the Laratrust configuration is correct as per the official documentation.

Has anyone else experienced this issue after upgrading to Laravel 10 and Laratrust 8.5? Any suggestions for troubleshooting this error or resolving it?

1 like
2 replies
martinbean's avatar

@raihanuddin Your error message is giving you a description (“call to member function setRelation() on null”) and also the location it is happening (line 219 of Laratrust’s UserDefaultChecker.php file), so it’s usually a good idea to start there and work backwards.

The error is pointing you to here:

https://github.com/santigarcor/laratrust/blob/22a6c817391061bf8e007af04d069778165edef1/src/Checkers/User/UserDefaultChecker.php#L219

And as you can see, the line there is a setRelation method call. But the error is telling you that’s happening on null value. So, work up. The method is being called on a $role variable, so look for where that is defined. It’s defined on line 213 via $role = new $class. So again, work backwards. Where does $class come from? Well, it’s a string and passed in as an argument to the hydrateRole method. So this suggests to me you’ve not updated a configuration value somewhere with the correct value, hence getting null instead of a model instance.

raihanuddin's avatar

@martinbean here is my laratrust.php, any other config causes issue?

Please or to participate in this conversation.