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',
];
}
2
Answers
Its because your
$staffAccess
isnull
. So the program turn an error becausein_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
isnull
the program won’t able enter yourreturn $next($request);
. You can check it withis_null
You need to check the array for its presence. You can make it a condition or a ternary operator: