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

Bogey's avatar
Level 1

Confusing eloquent moment (And spatie permissions)

This would either belong in laravel or eloquent, I don't know... but I'm testing some ideas and running the following code:

Auth::user()->with(['roles'])->get()->toArray()

Which I thought would retrieve the logged in user's data with his roles attached, but it's retrieving every user that's in the database with their roles...

0 likes
5 replies
Bogey's avatar
Level 1

Another test I'm doing is putting the following code in the __constructor() method to have people that don't have the role be redirected elsewhere

if(Auth::user()->hasRole('admin'))
{
    return redirect(route('home'));
}

That provides me with the following error

Call to a member function hasRole() on null

What am I doing wrong?

The route

Route::group(['auth', 'middleware' => ['role:admin']], function () {
    Route::get('/admin', [AdminController::class, 'index'])->name('admin.index');
});

And the controller

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{

    public function __construct()
    {
        // Making sure the user is authorized to be here
        if(Auth::user()->hasRole('admin'))
        {
            return redirect(route('home'));
        }
    }

    public function index()
    {
        echo '<pre>';
        print_r(Auth::user()->with(['roles'])->get()->toArray());
        echo '</pre>';
    }

}

The ['role:admin'] middleware works, but I'm confused why the 'hasRole' thing isn't working either.

DhPandya's avatar
DhPandya
Best Answer
Level 12

Auth Facade yet not initiated in the constructor of the controller. So it will return null. That is why it shows an error that Call to a member function hasRole() on null Try to shift your constructor logic to any middleware. For the redirection purpose, Laravel has already a middleware RedirectIfAuthenticated.

1 like
Bogey's avatar
Level 1

@DhPandya that makes sense I guess. That answers that question.

Why does the following retrieve every users?

print_r(Auth::user()->with(['roles'])->get()->toArray());

Is the Auth::user() just a reference to the User model? I thought it was specifically to the user that was authenticated.

DhPandya's avatar

@Bogey

Auth::user()->with(['roles'])->get()->toArray()  // Retrive all the user.
Auth::user()->with(['roles']) //Retrieve only the current logged in user.

If you found your answer mark the question as solved.

Bogey's avatar
Level 1

@DhPandya Running the following gives me an error

// Allowed memory size of 536870912 bytes exhausted (tried to allocate 257953792 bytes)
echo '<pre>';
print_r(Auth::user()->with(['roles']));
echo '</pre>';

[EDIT]

Auth::user()->roles->pluck('name')

That is how you retrieve the logged in user's roles.

1 like

Please or to participate in this conversation.