Search code examples
phplaravelfunctionlaravel-5.3laravel-query-builder

Writing a function in laravel


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.


Solution

  • 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) { ... }
    }