Dynamic Where clause in query with eloquent in Laravel 5.2

public function getObjs($enquiry_status = null, $enquiry_case_type = null, $from_date = null, $to_date = null)
{
    $query = Enquiry::query();
    if(isset($enquiry_status) && $enquiry_status != null){
        $query = $query->where('status', $enquiry_status);
    }
    if(isset($enquiry_case_type) && $enquiry_case_type != null){
        $query = $query->where('case_type', '$enquiry_case_type');
    }
    if(isset($from_date) && $from_date != null){

        $query = $query->where('date', '>' , '$from_date');
    }
    if(isset($to_date) && $to_date != null){
        $query = $query->where('date', '<', '$to_date');
    }
    $query = $query->whereNull('deleted_at');
    $objs = $query->get();
    return $objs;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s