Home > Mobile >  Laravel Spatie/Laravel-Permission Failed to fetch role data
Laravel Spatie/Laravel-Permission Failed to fetch role data

Time:02-24

what im trying to do is fetcing role data from single actions controller and got error messege when i test it out from postman. "message": "Call to undefined method App\Models\User::auth()", for anyone can give me hint or solution to fix this problems will highly appriciate. for further information im using jwt for auth, api as the guard.

so let me show you my code.

Controller:

<?php

 namespace App\Http\Controllers\Api\Admin;

 use App\Http\Controllers\Controller;
 use Illuminate\Http\Request;
 use App\Models\User;

 class RoleController extends Controller
 {
   public function __invoke()
   {
      return User::auth()->user()->getRoleNames();
   }

 }

Model:

<?php

 namespace App\Models;

 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Tymon\JWTAuth\Contracts\JWTSubject;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;
 use Spatie\Permission\Traits\HasRoles;


class User extends Authenticatable implements JWTSubject
{
 use HasFactory, Notifiable, HasRoles;


protected $guard_name = "api";
/**
 * 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',
];

public function getJWTIdentifier(){
    return $this->getKey();
}

public function getJWTCustomClaims(){
    return [];
}


}

routes:

//group route with prefix "admin"
Route::prefix('admin')->group(function () {

//route login
Route::post('/login', [App\Http\Controllers\Api\Admin\LoginController::class, 'index']);

//group route with middleware "auth"
Route::group(['middleware' => 'auth:api'], function() {

    //data user
    Route::get('/user', [App\Http\Controllers\Api\Admin\LoginController::class, 'getUser']);

    //refresh token JWT
    Route::get('/refresh', [App\Http\Controllers\Api\Admin\LoginController::class, 'refreshToken']);

    //logout
    Route::post('/logout', [App\Http\Controllers\Api\Admin\LoginController::class, 'logout']);

    Route::prefix('authorization')->group(function () {
        Route::get('/roles', RoleController::class);// not working
    });

});


});

CodePudding user response:

There are few ways you can try for getting those roles:

shows all the role names

Auth::user()->roles
$roles = $user->getRoleNames(); // Returns a collection

check user has specific role

Auth::user()->hasRole('admin')

check user has any roles

Auth::user()->hasAnyRole(['super_admin', 'vendor'])

For other usage take a look at the site: Spatie roles and permission info

  • Related