Adding roles to currently loggedin user programmatically

Drupal 8

Add roles to logged in current user programmatically 

 

$uid = Drupal::currentUser()->id();
$user = Drupal\user\Entity\User::load($uid);
$user->addRole('rolename');
$user->save();