@mac
In order to check the user's role and show select option in Laravel, you can use the following approach:
- First, you need to create a role column in your users table to store the user's role.
- Define the role constants in your User model or in a separate Role.php file:
1
2
3
4
|
class Roles {
const ADMIN = 'admin';
const USER = 'user';
}
|
- Use middleware to check the user's role before accessing the page where you want to show the select option. For example:
1
2
3
|
Route::group(['middleware' => ['auth', 'role:admin']], function() {
// This route can only be accessed by users with the admin role
});
|
- In your blade view file, you can show the select option based on the user's role:
1
2
3
4
|
<select name="role" id="role">
<option value="{{ Roles::ADMIN }}" @if(Auth::user()->role == Roles::ADMIN) selected @endif>Admin</option>
<option value="{{ Roles::USER }}" @if(Auth::user()->role == Roles::USER) selected @endif>User</option>
</select>
|
- Make sure to update the user's role when they change it in the select option:
1
2
3
4
5
6
7
|
public function updateRole(Request $request) {
$user = Auth::user();
$user->role = $request->input('role');
$user->save();
return redirect()->back();
}
|
By following these steps, you can check the user's role and show the select option in Laravel.