Last active
December 28, 2018 07:50
-
-
Save savankaneriya/7efed30d9bd8aee425c0a41ec7247485 to your computer and use it in GitHub Desktop.
Role middleware which accepts more than one role in laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'roles' => [ | |
'super admin' => 1, | |
'admin' => 2, | |
'user' => 3, | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Facades\Auth; | |
class Role | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next, ... $roles) | |
{ | |
$userRoles = config('user.roles'); | |
$user = Auth::user(); | |
//dd($roles[$role]); | |
foreach($roles as $role) { | |
// Check if user has the role This check will depend on how your roles are set up | |
//dd($userRoles[$role]); | |
if($user->role == $userRoles[$role]){ | |
return $next($request); | |
} | |
} | |
return redirect('login'); | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Route::group(['prefix' => 'franchise', 'middleware' => 'role:franchise'], function()
{
....
....
});