Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trait 'App\HasRoles' not found error in laravel 5.6

I"m try to role permission system by laravel 5.6

when i try to register with default auth(php artisan make:auth) i get this error

"Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Trait 'App\HasRoles' not found"

this my model user.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;

    protected $guard_name = 'web'; 

    /**
     * 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',
    ];
}
like image 963
rehan Avatar asked Oct 02 '18 20:10

rehan


People also ask

What is the use of trait in Laravel?

By adding their HasRoles Trait to our classes, we can then use the Trait's functionality to manage roles. Additionally, the Laravel framework itself uses Traits to layer functionality and complexity when needed. Notable examples that you might run across include AuthenticatesUsers and Notifiable.

What is exception handling in Laravel?

Exception is an event that occur during the execution of program and disrupt the normal flow of instructions. In Laravel, there are handler class through which you can handle all the exception in your application. report () method is used to log exceptions and render () method is used to convert exception in HTTP response.

How to get started with Laravel project?

Let us evoke our first step to get started. Generically, i invoke my first step by installing the new laravel application. However, if you have done this task already, then you can skip it and directly move to the second step. Run the command to start the project installation. Right after the project installation, get inside the project directory.

How to connect Laravel to database with refined consensus?

Run the command to start the project installation. Right after the project installation, get inside the project directory. Please insert the following code in .env file, and it evokes the connection between laravel and database with refined consensus.


3 Answers

Try this. Import HasRoles package as follow;

use Spatie\Permission\Traits\HasRoles;
like image 22
sanduniYW Avatar answered Sep 27 '22 17:09

sanduniYW


I'm guessing you're using the Spatie Laravel Permission package.

If so, you need to import the class like so:

use Spatie\Permission\Traits\HasRoles;

like image 81
Paras Avatar answered Sep 27 '22 18:09

Paras


Run this command

composer install

like image 43
Abdalltif Basher Avatar answered Sep 27 '22 18:09

Abdalltif Basher