1

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');
}
6
  • Your second parameter $access_pass in the in_array is null. Double check your web.php config. Commented Jul 7, 2022 at 21:06
  • here it is 'admins_only' => [ 'test' ], Commented Jul 7, 2022 at 21:19
  • From what I see, you would have to have config/web.php file and inside return ['offlinePass' => ['array', 'of', 'values']];. Variable admins_only is used in session from what I see. Commented Jul 7, 2022 at 21:37
  • It works, now I got this error. Call to undefined function App\Http\Controllers\save() Commented Jul 7, 2022 at 21:41
  • That's a different issue. Check your route, looks like you didn't define it properly. Commented Jul 7, 2022 at 21:46

3 Answers 3

6

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 ?? [])
Sign up to request clarification or add additional context in comments.

Comments

0

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()

Comments

0

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

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

2 Comments

How does your answer relate to the question?
in this example the $access_pass = config('web.offlinePass'); not array . you check by function dd($access_pass); , for that you need to change to array by function toArray() to check is in array or not

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.