Laravel 5.2 : How to get the role of current user when using `Zizaco/entrust`?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Laravel 5.2 & Zizaco/Entrust: Retrieving User Roles Effectively As developers working within the expansive ecosystem of Laravel, integrating third-party packages is a common practice to extend core functionality. The `Zizaco/entrust` package offers a powerful way to manage roles and permissions, which is invaluable for building robust authorization systems. However, when dealing with specific versions like Laravel 5.2 and custom trait implementations, accessing related data—like a user's roles—can sometimes lead to unexpected errors. This post addresses a common hurdle: how to reliably retrieve the roles of the currently authenticated user when using `Zizaco/entrust` in an older Laravel environment. We will dissect the provided example, identify the likely cause of the error, and establish a robust pattern for accessing user data. ## Understanding the Entrust Structure The `Zizaco/entrust` package relies heavily on Eloquent relationships to link users to their respective roles. When working with custom traits like `EntrustUserTrait`, it is crucial to ensure that the necessary relationships are correctly defined and loaded when an instance of the model is retrieved from the database. In your provided setup, you attempted to access roles via `$user->roles->first()->name`. The error `Trying to get property of non-object` strongly suggests that either the `roles` relationship was not properly defined in your Eloquent model or the necessary data wasn't loaded when accessing it within your service layer. To solve this, we must ensure our service layer interacts with the Eloquent model correctly, leveraging Laravel’s powerful ORM capabilities. ## The Solution: Leveraging Eloquent Relationships Instead of manually chaining deep property access in a service class that relies on an authenticated user, the safest and most idiomatic approach is to rely directly on the loaded relationship defined within the `User` model. This pattern aligns perfectly with the principles of clean data access advocated by Laravel best practices. ### Refined Model Setup First, let's ensure your `User` model properly defines the relationship to its roles. Assuming you have a standard many-to-many relationship setup for roles (which is common in authorization systems), the fix often lies in defining this relationship correctly in the model itself. For demonstration purposes, if your `Role` model has a pivot table linking it to users, ensure your `User` model defines the relationship: ```php // app/Models/User.php (or wherever your User model resides) use Illuminate\Database\Eloquent\Model; use Zizaco\Entrust\Traits\EntrustUserTrait; class User extends Model // Assuming Eloquent base { use EntrustUserTrait; /** * Define the relationship to roles. This is crucial for correct loading. */ public function roles() { // Assuming a pivot structure linking users and roles return $this->belongsToMany(Role::class); } // ... other model methods } ``` By explicitly defining the `roles()` relationship, you instruct Eloquent how to fetch this related data when you call `$user->roles`. ### Implementing the Data Retrieval Service With the correct Eloquent relationship in place, your service class can now safely retrieve the necessary information. We will inject the authenticated user directly and access the relationship cleanly. ```php namespace App\Services; use App\User; use Zizaco\Entrust\EntrustRole; use Illuminate\Support\Facades\Auth; // Use Auth facade for current user management class NameAndRole { public $username; public $roleName; /** * Retrieves the username and the first role name of the current authenticated user. */ public function getRoles() { if (!Auth::check()) { return ['error' => 'User not authenticated']; } $user = Auth::user(); // Safely access username $this->username = $user->name; // Safely access the first role name via the defined relationship if ($user->roles && $user->roles->isNotEmpty()) { $this->roleName = $user->roles->first()->name; } else { $this->roleName = 'No role assigned'; } return [ 'username' => $this->username, 'role' => $this->roleName ]; } } ``` ### Using the Service in the View When injecting this service into a Blade view, you should access the method directly rather than relying on complex injection if possible. The error in your original example likely stemmed from trying to use `@inject` with an object that wasn't fully initialized or structured correctly for that context. In your `sidebar.blade.php`: ```blade {{-- Assuming you have properly injected and called the method --}} @php $nameAndRoleService = app(\App\Services\NameAndRole::class); $userData = $nameAndRoleService->getRoles(); @endphp
Username: {{ $userData['username'] ?? 'N/A' }}
Role: {{ $userData['role'] ?? 'N/A' }}
``` ## Conclusion Retrieving user roles using `Zizaco/entrust` in Laravel requires a deep understanding of Eloquent relationships, especially when extending core models with traits. The initial error highlights the necessity of ensuring that relationships are explicitly defined and loaded correctly before attempting to traverse them in service layers. By defining the `roles()` relationship in your `User` model and using robust checks within your service class, you establish a resilient pattern for data access. For further guidance on structuring complex Eloquent interactions, always refer to official Laravel documentation; mastering these foundational concepts is key to building scalable applications, much like those promoted by the spirit of modern PHP frameworks like Laravel.