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

sunrise's avatar

How to change users' role?

I am using Laravel 5.2 and Zizaco/entrust 5.2,
How to change users' role?
For example:
users

id      name         email                   password 
1       Jim          Jim@example.com         ******

roles

id       name 
1        vip
2        ordinary

role_user

user_id      role_id
1                1

Jim's role is vip ,and the vip role will expire in 30 days,how to change vip to ordinary when expired?

0 likes
3 replies
Jaytee's avatar

Well you could have a check to see if the 30 days are up, if the 30 days are up, detach() the role from the user and attach() the ordinary role.

Example:

You could have a column on your User's table or User Role table called expires_at

if ($expires_at is 30 days) {
    $user->roles()->detach(1); // Detach VIP role
    $user->roles()->attach(2); // Attach Ordinary role
}

Note that of course you'd need to make a proper check.

1 like
sunrise's avatar

@DPJack Thanks,and how to make a check? check it everyday and check it when user login,is it ok? but I've no idea how to do it.

Jaytee's avatar

I'd recommend creating an Artisan command that runs daily (or whatever timeframe you prefer) or find your own way.

Check the documentation out here:

Artisan Console Commands: https://laravel.com/docs/5.2/scheduling Task Scheduling (to automate): https://laravel.com/docs/5.2/scheduling

so basically the check would be like:

if the current time is less than the expiry time: Then the users' membership is still valid else: The users' membership is expired, remove and add a new role

1 like

Please or to participate in this conversation.