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

faraz73's avatar

error : trait is not found (using in models)

Hi, I have some trait that I want to use it in my models.

here I have two traits that I want to use :

my_problem

the error is: a trait is not found.

Imgur

<?php

namespace App\Models;



use App\Models\Services\Permissions\Traits\HasPermissions;
use App\Models\Services\Roles\Traits\HasRoles;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;


class User extends Authenticatable
{
    use HasFactory, Notifiable , HasRoles , HasPermissions;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


}
<?php

namespace App\Models\Services\Permissions\Traits;

use App\Models\Permission;
use Illuminate\Support\Arr;

trait HasPermissions
{
    /* ===== permissions ===== */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

    protected
    function getAllPermissions(array $permissions)
    {
        return Permission::whereIn('name', Arr::flatten($permissions))->get();
    }

    public function givePermissions(... $permissions)
    {
        $permissions = $this->getAllParmissions($permissions);

        if ($permissions->isEmpty()) return $this;

        $this->permissions()->syncWithoutDetaching($permissions);

        return $this;

    }

    public function withdrawPermissions(... $permissions)
    {
        $permissions = $this->getAllParmissions($permissions);
        $this->permissions()->detach($permissions);
        return $this;
    }

    public function refreshPermissions(... $permissions)
    {
        $permissions = $this->getAllParmissions(Arr::flatten($permissions));
        $this->permissions()->sync($permissions);
        return $this;
    }

    public function hasPermission(Permission $permissions)
    {
        return $this->permissions->contains($permissions);
    }
}
<?php

namespace App\Models\Services\Roles\Traits;

use App\Models\Role;
use Illuminate\Support\Arr;
trait HasRoles
{
    /* ==== roles ==== */


    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

    protected function getAllRoles(array $roles)
    {
        return Role::whereIn('name', Arr::flatten($roles))->get();
    }

    public function giveRoles(... $roles)
    {
        $roles = $this->getAllRoles($roles);

        if ($roles->isEmpty()) return $this;
        $this->Roles()->syncWithoutDetaching($roles);

        return $this;
    }

    public function withdrawRoles(... $roles)
    {
        $roles = $this->getAllRoles($roles);

        $this->roles()->detach($roles);

        return $this;
    }

    public function refreshRoles(... $roles)
    {
        $roles = $this->getAllRoles($roles);
        $this->roles()->sync($roles);
        return $this;
    }

    public function hasRole(string $role)
    {
        return $this->roles->contains('name', $role);
    }
}
0 likes
24 replies
kkatwork's avatar

Try giving the full namespace of trait directly in the use statement, like

use App\Models\Services\Permissions\Traits\HasPermissions; 
faraz73's avatar

i did look at first image User.php line : 6

faraz73's avatar

guys .. little Help here .. please

faraz73's avatar

the current IDE doesn't support the return data type method syntax like

function fn() :bool { ... }

that is not so serious, I just ignored it.

Sinnbeck's avatar

And where did you find that code? I dont think that is valid php? Thee new fn() would be something like

$my_function = fn($a) : bool => $a;
faraz73's avatar

it is valid .. but removing it did not solve the problem, unfortunately.

Sinnbeck's avatar

Ok interesting. I cannot get that code to run in php8. Oh well. Are there any errors in then file now? Try running `composer dump-autoload' again

Sinnbeck's avatar

Can you load the other trait, if you comment out the HasPermissions one ?

faraz73's avatar

Yes! HasRoles trait can use perfectly .. but HasPermissions

Sinnbeck's avatar

Can you post the code for the class so we can test it out? In your original screenshot I see no errors besides the red error icon

Sinnbeck's avatar

Sadly I cannot recreate the error. Try deleting the old HasPermissions file and creating a new. Now make sure that it works.. Then copy in the class definition and a single method, and see if it works

faraz73's avatar

it is a practice (thank god !)... I shouldn't take such error logically .. or I'm doing something wrong.

Please or to participate in this conversation.