Writing a function in laravel

All we need is an easy explanation of the problem, so here it is.

I have the following function which fetch some data related to jobs from database. The user can search for jobs with job title / keyword, city and/or category. The user can either choose one option, e.g. searching jobs only by title, or by category. or he can use all options for deep search. Below is my function:

public function jobsearch(Request $request)
    {
        $keyword = htmlspecialchars($request->input('keyword'));
        $city_id = $request->input('city_id');
        $category_id = $request->input('category_id');

        if($keyword !== '' && $city_id != 0 && $category_id == 0)
        {
        $data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('city_id', $city_id)->get();
        } 
        elseif($keyword !== '' && $city_id == 0 && $category_id != 0) 
        {
        $data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->get();
        } 
        elseif($keyword == '' && $city_id != 0 && $category_id != 0)
        {
        $data = DB::table('job_details')->where('category_id', $category_id)->where('city_id', $city_id)->get();
        }
        elseif($keyword !== '' && $city_id == 0 && $category_id == 0)
        {
        $data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->get();
        }
        elseif($keyword == '' && $city_id == 0 && $category_id != 0)
        {
        $data = DB::table('job_details')->where('category_id', $category_id)->get();
        }
        elseif($keyword == '' && $city_id != 0 && $category_id == 0)
        {
        $data = DB::table('job_details')->where('city_id', $city_id)->get();
        }
        else
        {
        $data = DB::table('job_details')->where('job_title', 'like', '%'.$keyword.'%')->where('category_id', $category_id)->where('city_id', $city_id)->get();
        }

        foreach($data as $data)
        {
            echo $data->job_title.'<br>';
        }

    } 

As you can see the function is too much messy with many if and elseif statements. My question is if there is any way to write the given function in clean way? How would you write the given function in your style? Please Help.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You’re really missing out on the best parts of Laravel’s query builder.

public function jobsearch(Request $request) {
    // htmlspecialchars makes no sense here
    $keyword = $request->input('keyword');
    $city_id = $request->input('city_id');
    $category_id = $request->input('category_id');

    $query = DB::table('job_details');

    if($keyword) {
        $query->where('job_title', 'like', '%'.$keyword.'%');
    }

    if($city_id) {
        $query->where('city_id', $city_id);
    }

    if($category_id) {
        $query->where('category_id', $category_id);
    }

    $results = $query->get();

    foreach($data as $data) { ... }
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply