The application is built in Laravel. I am trying to insert few more search filter into the existing one. some search filter works and some doesn’t. Can you please look into my code and correct me at where i am doing wrong?
This is how my page looks. Previously there were only 2 search fields Reference and keyword . I have included 4 other search field called Sector , Business Status , Turnover and a sort by .
The Sector and Turnover search filter are working while Business status field and Sortby field are not working. And i am out of clue why?
> <form method="get" action="" class="">
> <label for="cat">
> <p style="margin-top: 6px;">
> <b>Sector</b>
> </p>
> </label>
> <select class="form-control" name="cat" id="cat">
> <option value="" label="Please select">Please select</option>
> <option value="1" label="Technology" <?php if(isset($_GET["cat"]) && $_GET["cat"] == 1){ echo 'selected="selected"';} ?>>Technology</option>
> <option value="12" label="Medical" <?php if(isset($_GET["cat"]) && $_GET["cat"] == 12){ echo 'selected="selected"';} ?>>Medical</option>
> ...
> </select>
> <select class="form-control" id="business_status" name="business_status" aria-describedby="business_status">
> <option value="" label="Please select">Please select</option>
> <option value="1" label="Live" <?php if(isset($_GET["business_status"]) && $_GET["business_status"] == 1){ echo 'selected="selected"';} ?>>Live</option>
> <option value="2" label="Sold" <?php if(isset($_GET["business_status"]) && $_GET["business_status"] == 2){ echo 'selected="selected"';} ?>>Sold</option>
> --
> </select>
> <select name="turnover" id="turnover">
> <option value="" label="Please select">Please select</option>
> <option value="1" label="Up to £500k" <?php if(isset($_GET["turnover"]) && $_GET["turnover"] == 1){ echo 'selected="selected"';} ?>>Up to £500k</option>
> <option value="2" label="£500k - £1m" <?php if(isset($_GET["turnover"]) && $_GET["turnover"] == 2){ echo 'selected="selected"';} ?>>£500k - £1m</option>
> --
> </select>
> <select class="form-control" id="sortby" name="sortby" aria-describedby="sortby" >
> <option value="0" <?php if(isset($_GET["sortby"]) && $_GET["sortby"] == 0){ echo 'selected="selected"';} ?> >Newest To Oldest</option>
> <option value="1" <?php if(isset($_GET["sortby"]) && $_GET["sortby"] == 1){ echo 'selected="selected"';} ?> >Oldest To Newest</option>
> --
> </select>
> <button type="submit">Search</button>
> </form>
> ```
and this is the code under app/businesses.php page
> public function scopeSearch($query, $request)
> {
> if (isset($request->cat)) {
> $query->where('company_category_id', $request->cat);
> }
> if (isset($request->turnover)) {
> $turnoverrange = $request->turnover;
> if ($turnoverrange == 1) {
> $query->where('turnover', '<=', 300000);
> } else if ($turnoverrange == 2) {
> $query->where('turnover', '>=', 300000);
> $query->where('turnover', '<=', 1000000);
> }
> }
> if (isset($request->business_status)) {
> $query->where('company_status_id', $request->business_status);
> }
> if (isset($request->sortby)) {
> $sortbyvalue = $request->sortby;
> if ($sortbyvalue == 0) {
> $query->orderby('updated', 'desc');
> } else if ($sortbyvalue == 1) {
> $query->orderby('updated', 'asc');
> } else if ($sortbyvalue == 2) {
> $query->orderby('turnover', 'asc');
> } else if ($sortbyvalue == 3 ) {
> $query->orderby('turnover', 'desc');
> }
> }
> return $query;
> }
The database table is company
![image|690x296](upload://7musEHt6pNQ0YjL8b5gzxSKifbj.jpeg)
please advise me what I am doing wrong. Specially I dont understand why the sort by feature is not working as it is checking data under same table
The code in controller is like this
> namespace App\Http\Controllers;
> use Illuminate\Http\Request;
> use Illuminate\Support\Facades\View;
> use App\User;
> use App\Businesses;
> ...
> use Illuminate\Support\Facades\Redirect;
> use App\CancellationRequest;