I am working with Scope Search and cannot figure out why my ternary operator is working like it is.
I have the following in my view:
<form action='/search-results' method='GET'>
{{ csrf_field() }}
<div style="background-color:white">
<div class="container py-5">
<div class="input-group custom-search-form md-form mt-0">
<input type="text" class="form-control" name="city" placeholder="City...">
<input type="text" class="form-control" name="search" placeholder="Search...">
<span class="input-group-btn">
<button class="btn btn-default-sm" type="submit">
Submit
</button>
</div>
</div>
</div>
</form>
I have the following in my Controller:
public function index()
{
{
//The search() function is accessed using 'scopeSearch' within NPIData Model
$providers = NPIData::lookup()->orderBy('NPI')->paginate(20);
return view('inc.searchresults', compact('providers'));
}
I have the following in my NPIData model:
public function scopeLookup($query)
{
//this says if the request is NOT empty, return $query (which would be an empty query), otherwise return $query with all the conditions listed
return empty(request()) ? $query : $query->where('NPI', 'like', '%'.request()->search.'%')
->where('Entity Type Code', '=', '1')
->where('Provider Business Mailing Address City Name', '=', request()->zip)
->orWhere('Provider First Name', 'like', '%'.request()->search.'%')
->orWhere('Provider Last Name (Legal Name)', 'like', '%'.request()->search.'%');
}
}
When a search term is entered in both the 'city' and 'search' text inputs, the $query executes correctly. However, if one or both inputs are left blank, the $query returns everything from my NPIData table. If one or both inputs are left blank, I want my $query to return nothing so the search results are blank. I assume I have to change something in the second part of my ternary operator but cannot figure out what that should be.