Laravel在哪里条件

时间:2015-08-05 15:57:06

标签: php laravel laravel-4

$location = $details['location'];
    $specializationAreasName = $details['specialization_areas'];
    if($details['location'] == 'all'){
        $location = '';
    }
    if($specializationAreasName == 'all'){
        $specializationAreas = '1 = 1';
    }
    else{
        $specializationAreas = "'specialization_areas','like', '%'".$specializationAreasName."'%'";
    }

    $result = Lawyers :: with('SkillsMappedToLawyer')
                        ->where('location',$location)
                        ->where($specializationAreas)
                        ->take(10)->skip($skip)
                        ->get();

请找到我的代码如果位置==所有我想要搜索全部,否则我需要搜索动态的标准。 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

$location = $details['location'];
$specializationAreasName = $details['specialization_areas'];

$query = Lawyers :: with('SkillsMappedToLawyer');

if($details['location'] != 'all'){
    $query = $query->where('location', $location);
}

if($specializationAreasName != 'all'){
    $query = $query->where('specialization_areas', 'LIKE', '%'.$specializationAreasName.'%');
}


$result = $query
             ->take(10)->skip($skip)
             ->get();