if else statements are not working when I add an else statement. php

one text

Solution:

The else can possibly short cut any other matches because of the return in the else block.

For example, if you search for jane, and this is not anywhere in the first $userData entry will go to the else: no firstName match, no lastName match, no address match, so then the else returns the view to no results (which also exits the foreach) and does not continue searching for other $userData entries.

This example should work as expected:

function search($request) {
    $user = DB::table('subscribers')->get();
    $input = $request->input('query');
    $fillter = [];
    
    foreach ($user as $userData) {
    
        if (strtoupper($userData->firstName) == strtoupper($input)) {
            $firstUser = Subscriber::where('firstName', $input)->get();
            array_push($fillter, $firstUser);
    
        } else if (strtoupper($userData->lastName) == strtoupper($input)) {
            $lastUser = Subscriber::where('lastName', $input)->get();
            array_push($fillter, $lastUser);
    
        } else if (strtoupper($userData->address) == strtoupper($input)) {
            $addressUser = Subscriber::where('address', $input)->get();
            array_push($fillter, $addressUser);
    
        }
    }
    
    if (!empty($fillter)) {
        return view('pages/results', compact('fillter'));
    } else {
        return view('pages/empty', compact('fillter'));
    }
}

Source