Defining User role model in Laravel
app/Enums/UserRole.php
<?php
namespace App\Enums;
enum UserRole: string
{
case Admin = ‘admin’;
case Manager = ‘manager’;
case Customer = ‘customer’;
}
Add Your Heading Tapp/Models/User.php Here is your complete User model code
<?php
namespace App\Models;
use App\Enums\UserRole;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*/
protected $fillable = [
‘name’,
’email’,
‘password’,
‘role’,
];
/**
* The attributes that should be hidden for arrays.
*/
protected $hidden = [
‘password’,
‘remember_token’,
];
/**
* The attributes that should be cast.
*/
protected $casts = [
’email_verified_at’ => ‘datetime’,
‘role’ => UserRole::class, // 🎯 Enum casting
];
/**
* Check if the user has a specific role.
*/
public function hasRole(UserRole $role): bool
{
return $this->role === $role;
}
/**
* Quick role checks
*/
public function isAdmin(): bool
{
return $this->role === UserRole::Admin;
}
public function isManager(): bool
{
return $this->role === UserRole::Manager;
}
public function isCustomer(): bool
{
return $this->role === UserRole::Customer;
}
/**
* Example relationship (optional)
*/
// public function posts()
// {
// return $this->hasMany(Post::class);
// }
}
Migration for users table (partial)
$table->string(‘role’)->default(‘customer’); // stores values like ‘admin’, ‘manager’
Seeding Example (optional)
use App\Models\User;
use App\Enums\UserRole;
User::create([
‘name’ => ‘Admin User’,
’email’ => ‘admin@example.com’,
‘password’ => bcrypt(‘password’),
‘role’ => UserRole::Admin,
]);
UI Label (optional) Use config/constants.php to show labels like this
return [
‘USER_ROLE_LABELS’ => [
‘admin’ => ‘Administrator’,
‘manager’ => ‘Manager’,
‘customer’ => ‘Customer’,
],
];
In Blade:
{{ config('constants.USER_ROLE_LABELS')[$user->role->value] ?? 'Unknown' }}1. Show content based on role
If you have used the UserRole enum and the helper methods in your model like isAdmin(), isManager():
@auth
@if(auth()->user()->isAdmin())
<p>Welcome, Admin!</p>
@elseif(auth()->user()->isManager())
<p>Welcome, Manager!</p>
@elseif(auth()->user()->isCustomer())
<p>Welcome, Valued Customer!</p>
@endif
@endauth2. Using Enum comparison in Blade
You can directly compare the enum like this:
@php use App\Enums\UserRole; @endphp
@if(auth()->user()->role === UserRole::Admin)
<p>You are an admin.</p>
@endif@php use App\Enums\UserRole; @endphp
<nav>
<ul>
<li><a href=”/dashboard”>Dashboard</a></li>
@if(auth()->check() && auth()->user()->role === UserRole::Admin)
<li><a href=”/admin/settings”>Admin Settings</a></li>
@endif
@if(auth()->check() && auth()->user()->role === UserRole::Manager)
<li><a href=”/manager/reports”>Manager Reports</a></li>
@endif
</ul>
</nav>
@php
use App\Enums\UserRole;
@endphp
<select name=”role” class=”form-control”>
@foreach(UserRole::cases() as $role)
<option value=”{{ $role->value }}” {{ old(‘role’, $user->role->value ?? ”) === $role->value ? ‘selected’ : ” }}>
{{ ucfirst(config(‘constants.USER_ROLE_LABELS’)[$role->value] ?? $role->value) }}
</option>
@endforeach
</select>
