laravel 9 in_array():参数#2($ haystack)必须是类型阵列,null给定的

发布于 2025-02-13 22:42:50 字数 524 浏览 0 评论 0原文

我正在研究Laravel 9项目,并且正在尝试添加员工访问密码阵列,但是每次输入正确的密码时,我都会遇到此错误: in_array():参数#2($ haystack)必须是类型数组,null给出的

是代码:

public function access(Request $request) 
{
    $access_pass = config('web.offlinePass');

    if (!in_array($request->pass, $access_pass))

        return back()->withErrors(['The password entered is incorrect!']);

    session()->put('admins_only', $request->pass);
    
    session()-save();

    return redirect()->route('website.dash.dash');
}

I'm working on a Laravel 9 project and I'm trying to add arrays of staff access passcodes but I keep getting this error every time I type in the correct passcode: in_array(): Argument #2 ($haystack) must be of type array, null given

Heres the code:

public function access(Request $request) 
{
    $access_pass = config('web.offlinePass');

    if (!in_array($request->pass, $access_pass))

        return back()->withErrors(['The password entered is incorrect!']);

    session()->put('admins_only', $request->pass);
    
    session()-save();

    return redirect()->route('website.dash.dash');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2025-02-20 22:42:52

Pulluck方法返回一个集合,这不是数组。

$payaments = Equipment::where('vehicle_id', $id)->pluck('equipment_value')->toArray();

Pluck method return a collection, which is not an array.

$payaments = Equipment::where('vehicle_id', $id)->pluck('equipment_value')->toArray();
失与倦" 2025-02-20 22:42:51

替换带有空数组的值null的数组。 (当然只有在null的情况下):

in_array($request->pass, $access_pass ?? [])

Replace your array, which has the value null with an empty array. (of course only if it is null):

in_array($request->pass, $access_pass ?? [])
云巢 2025-02-20 22:42:51

您必须确保要检查的变量是一个数组,因此您可以使用print_r($ variable_name)在检查代码in_array()之前对其进行验证。

you have to make sure that the variable you are checking is an array, so you can verify it with print_r($variable_name) before checking code in_array()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文