0

I received this error in app/Http/Middleware/StaffAccess.php:22

$staff = Auth::guard('admin')->user();

$staffAccess = $staff->staff_access;

if (in_array($access, $staffAccess))
{
    return $next($request);
}

When I tried to login in dashboard as admin and redirect in dashboard I received this error

in_array() expects parameter 2 to be array, null given

Admin Model:

class Admin extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    // protected $casts = ['access'=>'array'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
    protected $casts = [
        'staff_access' => 'object',
    ];
}
1
  • If you have any differentiating flag/column b/w admin and admin staff users? Commented Oct 12, 2022 at 2:00

2 Answers 2

1

Its because your $staffAccess is null. So the program turn an error because in_array expect parameter to be array.

You can check the contains of $staffAccess or you can give a condition where $staffAccess == null, so if $staffAccess is null the program won't able enter your return $next($request);. You can check it with is_null

$staffAccess = $staff->staff_access;
if(!is_null($staffAccess)) { // or you can check with ($staffAccess === null)
 if(in_array($access, $staffAccess)){
    return $next($request);
 }
} else {
 // your other code
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to check the array for its presence. You can make it a condition or a ternary operator:

$staff = Auth::guard('admin')->user();

$staffAccess = $staff->staff_access;

if (in_array($access, $staffAccess ?? []))
{
    return $next($request);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.