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

lat4732's avatar
Level 12

A when() query doesn't work properly

Hey!

I have this query

ReviewReports::when(
                isset($reason) && $reason != "all",
                fn ($builder) => $builder->where('report_reason', '=', $reason)
            )->when(
                isset($status) && $status != "all",
                fn ($builder) => $builder->whereHas('review', fn ($builder) => $builder->where('report_status', $status))
            )->where('solved', 0)->orderBy('created_at', 'desc')->with('review', 'user')->get()

which isn't working properly. I'm not getting correct results. Is anything wrong with it?

That's the form

<form id="sortForm" autocomplete="off">
              <div class="row">
                <div class="col-12 col-md-2 col-lg-2">
                  <div class="form-group mb-0">
                    <label><i class="fa fa-sort"></i> Sort by reason</label>
                    <select class="form-control" id="selectReason">
                      <option value="all" disabled selected>All Reasons</option>
                      <option value="harmful_illegal" @if(request()->reason == "harmful_illegal") selected @endif>Harmful & Illegal</option>
                      <option value="personal_information" @if(request()->reason == "personal_information") selected @endif>Personal Information</option>
                      <option value="advertising_promotional" @if(request()->reason == "advertising_promotional") selected @endif>Advertising</option>
                      <option value="inappropriate_media" @if(request()->reason == "inappropriate_media") selected @endif>Inappropriate Media</option>
                      <option value="other" @if(request()->reason == "other") selected @endif>Other</option>
                    </select>
                  </div>
                </div>
                <div class="col-12 col-md-2 col-lg-2">
                  <div class="form-group mb-0">
                    <label><i class="fa fa-sort"></i> Sort by status</label>
                    <select class="form-control" id="selectStatus">
                      <option disabled selected>All Statuses</option>
                      <option value="0" @if(request()->status == "0") selected @endif>New Reports</option>
                      <option value="1" @if(request()->status == "1") selected @endif>Under Investigation</option>
                      <option value="2" @if(request()->status == "2") selected @endif>Investigation Finished</option>
                    </select>
                  </div>
                </div>
                @if(request()->reason || request()->status)
                <div class="col-12 col-md-2 col-lg-2">
                  <div style="padding-top: 30px;">
                    <a href="{{ route('admin.reviews.reports') }}"><button type="button" class="btn btn-danger">Reset</button></a>
                  </div>
                </div>
                @endif
              </div>
            </form>

I'm using jQuery to send the form.

      $("#selectReason").on('change', function() {
        var reason = $(this).val();
        var status = $("#selectStatus").val();

        if(reason == null) {
            reason = "all";
        }
        if(status == null) {
            status = "all";
        }

        window.location.replace("{{ route('admin.companies.reports') }}" + "/" + reason + "/" + status);
      });

      $("#selectStatus").on('change', function() {
        var reason = $("#selectReason").val();
        var status = $(this).val();

        if(reason == null) {
            reason = "all";
        }
        if(status == null) {
            status = "all";
        }

        window.location.replace("{{ route('admin.companies.reports') }}" + "/" + reason + "/" + status);
      });
0 likes
7 replies
Sinnbeck's avatar

How isnt it working? Error ? Wrong data (if so what is wrong with it)?

lat4732's avatar
Level 12

@Sinnbeck

I'm not getting correct results.

Wrong data. It's addressed only for the status filter. When I apply a status value I get the same (all) results.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Laralex can you add dd($reason, $status); just above the query and post what is returned?

Or check the query being run

$sql = ReviewReports::when(
                isset($reason) && $reason != "all",
                fn ($builder) => $builder->where('report_reason', '=', $reason)
            )->when(
                isset($status) && $status != "all",
                fn ($builder) => $builder->whereHas('review', fn ($builder) => $builder->where('report_status', $status))
            )->where('solved', 0)->orderBy('created_at', 'desc')->with('review', 'user')->tosql();
dd($sql);
1 like
lat4732's avatar
Level 12

@Sinnbeck Adding dd($reason, $status) leaded to an error for undefined $status. Then I saw that I'm actually a real idiot and forgot to pass the $status in the controller...

public function companiesReports($reason = null) {

to ⬇

public function companiesReports($reason = null, $status = null) {

I'll probably delete this thread. Thanks for the fast response :D

Sinnbeck's avatar

@Laralex Mark any answer and the thread will be long forgotten in a day :)

1 like

Please or to participate in this conversation.