Writing a function in laravel

空扰寡人 提交于 2019-12-13 09:24:04

问题


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.


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


来源:https://stackoverflow.com/questions/45511015/writing-a-function-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!