Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

Search query issue

Hi. I'm trying to search users using scopes but when trying to search for all, it only returns the wrong data. I.e I have in the searches form a checkbox with "All" "Active" "in-Active" but if I choose all it returns only the active for some reason.

// Controller 

    $users = User::role('Customer')
            ->findMailCentre($request->get('mail_centres'))
            ->HasStatus($request->get('status'))
            ->region($request->get('region'))
            ->type($request->get('type'))
            ->scans($request->get('scans'))
            ->sms($request->get('sms'))
            ->with(['account', 'settings'])
            ->get();

        return $users;


// Model scope

public function scopeHasStatus($query, $status_id)
    {
        if (!$status_id) {
            return $query;
        }
        return $query->whereHas('account', function ($query) use ($status_id) {
            $query->orWhere('status_id', $status_id);
        });
    }

// Blade file
            <h3>Status</h3>
            <div class="broadcastWrapper">
                <p style="text-transform: uppercase;"><input name="status[]" type="checkbox" id="all_statuses" /> ALL</p>

                <div id="statuses">
                    @foreach($statuses as $status)
                    <p style="text-transform: uppercase;"><input name="status[]" value="{{ $status->id }}" type="checkbox"/> {{ $status->title }}</p>
                    @endforeach
                </div>
            </div>

Any help would be grateful.

Many thanks

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

You're posting an array of status_id's

<input name="status[]" type="checkbox" id="all_statuses" />
<input name="status[]" value="{{ $status->id }}" type="checkbox"/> 

Either make your all_statuses button toggle all other checkboxes or give it a different value.

and change your query to whereIn:


public function scopeHasStatus($query, $status_id)
    {
        if (!$status_id) {
            return $query;
        }
        return $query->whereHas('account', function ($query) use ($status_id) {
            $query->whereIn('status_id', $status_id);
        });
    }
skoobi's avatar
Level 13

I think a good feature request for Laracast would be a "Are you sure you've checked your code?" Lol...

Thank you so much, I ended up just taking the value and name out of the all so it just grabs everything anyway.

Thank you

Please or to participate in this conversation.