Home Laravel Important laravel Sentinel Helper functions

Important laravel Sentinel Helper functions

by therichpost
2 comments
Laravel 7.2 routing with route group auth guard check with prefix

Hello to all, welcome to therichpost.com. In this post, I will tell you, Important laravel Sentinel Helper functions. I am doing with laravel first time in my website. Laravel is one of the top php mvc framework.

Here are the Important laravel Sentinel Helper functions and you can add these Sentinel Helper functions into your laravel helper.php file:

/**
* Return current user id
*/
function getUserId() {
if(Sentinel::check())
return Sentinel::getUser()->id;
}

/**
* Check user role
*/
function checkRole($role) {
if(Sentinel::check()){
if(Sentinel::inRole($role) && Session::get(‘activeRole’) == $role) {
return true;
}
}
return false;
}

/**
* get user role first
*/
function getUserRole() {
$roles = Sentinel::findById(getUserId())->roles;
foreach ($roles as $role) {
return $role->slug;
}
}

/**
* get active role from session
*/
function getActiveRole() {
if(Session::has(‘activeRole’))
return Session::get(‘activeRole’);
}

/**
* Get all user with admin role
*/
function getAdmins() {
$role = Sentinel::findRoleBySlug(‘admin’);
$users = $role->users()->with(‘roles’)->get();
return $users;
}

There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

You may also like

2 comments

alexa March 5, 2018 - 10:58 am

good post

Reply
therichpost May 15, 2018 - 3:51 pm

Thank you alexa

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.