How isnt it working? Error ? Wrong data (if so what is wrong with it)?
Apr 7, 2022
7
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);
});
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
Please or to participate in this conversation.