I created RoleEnum and cast it in User model. The role_id which is being cast to RoleEnum also has relationship to Role model.
<?php
namespace App\Enums;
enum RoleEnum:int
{
case SYSTEM_ADMINISTRATOR = 1;
case ADMINISTRATOR = 2;
case SAMPLE_ROLE_ONE = 3;
case SAMPLE_ROLE_TWO = 4;
case SAMPLE_ROLE_THREE = 5;
case SAMPLE_ROLE_FOUR = 6;
}
User Model
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'role_id' => RoleEnum::class,
];
public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}
And in my controller index
$users = User::with('role')
->paginate()
->withQueryString();
And I'm getting this error and it is pointing in paginate(). It must be using the class instead of the value.
Object of class App\Enums\RoleEnum could not be converted to string
Edit: After further testing, I found out that the error is caused by eager loading the role which is an Enum casted column and not by paginate(). I can simply remove the Enum casting and everything will work fine. But is there a way to cast an Enum and still use eager loading?
I suggest you create a Role table with the fields id, name, slug, level The migration should look like this
and the users table
you are free to personalize everything
Then, the relationships on the models must be managed depending on what you want, in these examples a user only has one role and I put the level field in order to be able to prioritize your roles later.
In the Role model, place these lines
In the User model, place these lines
Then you can do