简体   繁体   中英

PHP Laravel - If else condition with search filter

The application is built in laravel. I have inserted a new search filter called business status.

this is the output of my page

在此处输入图像描述

This is the code on the resource/view/business.blade.php page for Business Status drop down search filter

<form  method="get" action="" class="">
        <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> </form>

@foreach($Businesses as $Business)
    <tr>
    <td>
      ---
    </td>
    <td>--</td>
    
    <td>{{$Business->getStatus()}}</td>
   </tr>

This is the code in the backend that is app/businesses.php for the dropdown search

    public function scopeSearch($query, $request)
        {
                   
            if(isset($request->business_status)) {
                $query->where('company_status_id', $request->business_status);
            }      
            
                   
            $query->where('use_conf_box','=',0);
            return $query;
        }

And this is the code to write the business status in the below table

 public function getStatus(){
            if(is_null($this->company_status_id)){
                return "Awaiting Approval";
            } else{
                switch ($this->company_status_id) {
                case 1:
                    if($this->isExpired()){
                        return "Expired";
                        break;
                    }
                    if($this->isExpiring()){
                        return "Expiring Soon";
                        break;
                    }
                    return "Live";
                    break;
                case 2:
                    return "Sold";
                    break;
                case 3:
                   ---

                default:
                    return "Awaiting Approval";
                }
            }

The business whose status is Live they are categorized under few section. such as if a business expiry date is over then it is called expired. If a business expiry date is within 4 weeks then is is called expiring. this options are not inserted in the database. But the conditions are created as follows.

public function isLive(){
    if($this->is_approved == 1 && $this->company_status_id != 7 && $this->company_status_id != 6 && $this->company_status_id != 2 && $this->company_status_id != 3 && $this->company_status_id != 8){
        return true;
    } else {
        return false;
    }
}

public function isExpired(){
    if($this->expiry_date > date("Y-m-d")){
        return false;
    } else {
        return true;
    }
}

public function isExpiring(){
    if($this->expiry_date > date("Y-m-d", strtotime('+4 weeks'))){
        return false;
    } else {
        return true;
    }
}

How to bring those conditions under the filters. Currently all this is falling under live. As you see in the screenshot, if selected live it is also picking the business which is expired.

I want that business to show when some one will select expired.

The easiest way would be to create some scope for each of your test like this:

public function isExpired(){
    if($this->expiry_date > date("Y-m-d")){
        return false;
    } else {
        return true;
    }
}

public function scopeIsExpired($query){
    return $query->where('expiry_date', '>', now());
}

public function isLive(){
    if($this->is_approved == 1 && $this->company_status_id != 7 && $this->company_status_id != 6 && $this->company_status_id != 2 && $this->company_status_id != 3 && $this->company_status_id != 8){
        return true;
    } else {
        return false;
    }
}

public function scopeIsLive($query){
    return $query->notWhereIn('company_status_id', [7, 6, 2, 3, 8])->where('is_approved', 1);
}

then in your controller:

if($request->business_status == 'expired'){
    $query->isExpired();
}

if($request->business_status == 'live'){
    $query->isLive();
}

and so on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM