Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasRole does not exist Error #625

Closed
PardisZaryab opened this issue Apr 5, 2023 · 6 comments
Closed

hasRole does not exist Error #625

PardisZaryab opened this issue Apr 5, 2023 · 6 comments

Comments

@PardisZaryab
Copy link

Hello I still have problem with new Laratrust version, It give me (hasRole does not exist) error.
It create the user but when I write a condition like I used to do before to redirect each user to specific page it give me above mentioned error.
Please help me with this.
Thank you

@filippotoso
Copy link
Collaborator

Follow the documentation:

https://laratrust.santigarcor.me/docs/8.x/installation.html

  1. Add the Laratrust\Contracts\LaratrustUser interface and Laratrust\Traits\HasRolesAndPermissions trait in your user classes.
use Laratrust\Contracts\LaratrustUser;
use Laratrust\Traits\HasRolesAndPermissions;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements LaratrustUser
{
    use HasRolesAndPermissions;

    // ...
}

@PardisZaryab
Copy link
Author

I have done that and here is my user model and Dashboard file repository address.
https://github.com/PardisZaryab/zar

@filippotoso
Copy link
Collaborator

I just created a new Laravel installation, installed Laratrust per documentation and Auth::user()->hasRole() works perfectly.

What happens if you execute the following code at the beginning of the DashboardController::index() method?

dd(Auth::user());

@PardisZaryab
Copy link
Author

This is the result.

App\Models\User {#666 ▼ // app\Http\Controllers\DashboardController.php:14
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:8 [▶]
#original: array:8 [▶]
#changes: []
#casts: array:1 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: array:2 [▶]
#visible: []
#fillable: array:3 [▶]
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
#accessToken: null
}

@filippotoso
Copy link
Collaborator

What about:

dump(class_uses(Auth::user()));
dd(method_exists(Auth::user(), 'hasRole'));

@PardisZaryab
Copy link
Author

PardisZaryab commented Apr 5, 2023

I added
dd(Auth::user());
at the end of my dashboard code and it worked. Now it redirect to different dashboards.
Thank you so much for help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants