Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

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

enter image description here

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.

question from:https://stackoverflow.com/questions/65888539/php-laravel-if-else-condition-with-search-filter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...