Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abbasmashaddy72's avatar

Gate Define not working for other users except whose role_id is 1

The Below code in middleware is working fine when user has role_id 1, When I did the dd on role->permissions I get then the response is array.

But on this line

if (in_array($role->permissions, $permission)) {

I get this error for all other users whose role_id is different

in_array(): Argument #2 ($haystack) must be of type array, string given

My Roles Model has

protected $casts = [
        'permissions' => 'array',
    ];

My User Model has

protected function role()
    {
        return $this->hasOne(Roles::class, 'id', 'role_id');
    }

My web Middlewaregroup has

\App\Http\Middleware\RolePermissionCheck::class,

My Meddlware has

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;


class RolePermissionCheck
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!empty(Auth::user()->role_id)) {
            $role = Auth::user()->role;

            Gate::before(
                function () {
                    if (Auth::user()->role_id === 1) {
                        return true;
                    }
                }
            );
            // dd($role->permissions);
            foreach ($role->permissions as $permission) {
                Gate::define(
                    $permission,
                    function ($role) use ($permission) {
                        if (in_array($role->permissions, $permission)) {
                            return true;
                        }
                    }
                );
            }
        }

        return $next($request);
    }
}
0 likes
7 replies
abbasmashaddy72's avatar

After Changing it I am getting This error: in_array(): Argument #2 ($haystack) must be of type array, null given

MichalOravec's avatar

Your code doesn't ake any sence.

You loop through $role->permissions and then you check if a permission exist in the same array.

How I said use that package what I posted above.

abbasmashaddy72's avatar

I got your point I will try that package as you suggested but is there anything that can be done in the middleware so that the current code work fine.

abbasmashaddy72's avatar

Hi, I got the issue fixed. It is working fine now for all users.

if (!empty(Auth::user()->role_id)) {
            $role = Auth::user()->role;

            Gate::before(
                function () {
                    if (Auth::user()->role_id === 1) {
                        return true;
                    }
                }
            );
            foreach ($role->permissions as $permission) {
                Gate::define(
                    $permission,
                    function ($role) use ($permission) {
                        if (isset($permission[$role->permissions])) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                );
            }
        }

        return $next($request);

Please suggest me if there are changes that make it better.

Please or to participate in this conversation.