<?php

namespace App\Http\Middleware;

use Closure;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Traits\HasPermissions;

class HasPermission 
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next,...$permissions)
    {

        foreach($permissions as $permission){

       $permission = explode('|',$permission);



            if (!$request->user()->hasAnyPermission($permission)){
               
                return redirect('/');                        
            }
            break;
        }    
        return $next($request);
    }
}
