Laravel Query Builder - 其中(1 = 1)出错

时间:2015-09-16 15:47:31

标签: php laravel laravel-4 laravel-5 query-builder

我正在尝试将此PHP MySQL查询构建到Laravel Query构建器:

$sql = "SELECT employee_name, employee_salary, employee_position, employee_city, employee_extension, DATE_FORMAT(employee_joining_date, '%Y-%m-%d') as employee_joining_date, employee_age ";
$sql .= " FROM employee WHERE 1=1";
if (!empty($EmployeeNameSeach) ) {
    $sql .=" AND  employee_name LIKE '".$EmployeeNameSeach."%' ";    
}
if (!empty($SalarySeach)) {
    $sql .= " AND  employee_salary LIKE '".$SalarySeach."%' ";
}
if (!empty($PositionNameSeach) ) {
    $sql .= " AND  employee_position LIKE '".$PositionNameSeach."%' ";
}
if (!empty($CitySeach) ) {
    $sql .= " AND  employee_city LIKE '".$CitySeach."%' ";
}
if (!empty($ExtensionSeach) ) {
    $sql .= " AND  employee_extension LIKE '".$ExtensionSeach."%' ";
}
if (!empty($JoiningDataSeach) ) {
    $sql .= " AND  employee_joining_date LIKE '".$JoiningDataSeach."%' ";
}
if (!empty($AgeSeach) ) {
    $sql .= " AND  employee_age LIKE '".$AgeSeach."%' ";
}

我正在做的是 -

    $baseQuery  =   DB::table('employee')->select('employee_name', 'employee_salary', 'employee_position', 'employee_city', 'employee_extension',"employee_joining_date", 'employee_age', 'employee_id');
                        //->select('name as user_name', 'email')

    //Applying Filters
    $query  =   $baseQuery->where(1, '=', 1);
    if(!empty($EmployeeNameSeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$EmployeeNameSeach.'%');
    if(!empty($SalarySeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$SalarySeach.'%');
    if(!empty($PositionNameSeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$PositionNameSeach.'%');
    if(!empty($CitySeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$CitySeach.'%');
    if(!empty($ExtensionSeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$ExtensionSeach.'%');
    if(!empty($JoiningDataSeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$JoiningDataSeach.'%');
    if(!empty($AgeSeach))
        $query  =   $query->orWhere('employee_name', 'LIKE', '%'.$AgeSeach.'%');

    //Ordering and Limiting Current data
     $query =   $query->orderBy($columns[$column_index_for_ordering], $column_ordering_direction)
                ->skip($start_data_index)->take($per_page_content_number);
     $data          =   $query->get();

所以我收到错误 -

    $query  =   $baseQuery->where(1, '=', 1);

有人能帮帮我吗? 感谢。

2 个答案:

答案 0 :(得分:5)

你没有发布错误,但我猜你得到了:

  

找不到列:1054'where子句'中的未知列'1'

那是因为where方法的第一个参数应该是一个列名,并且会被引用。所以在你遇到的情况下:

$query = $baseQuery->where('1', '=', '1')

生成的SQL将是:

WHERE `1` = 1

所以你应该使用whereRaw来避免任何引用:

$query = $baseQuery->whereRaw('1 = 1');

答案 1 :(得分:3)

不需要WHERE 1 = 1部分。根本不需要。

此外,正如下面的评论所指出的,您要使用where,而不是orWhere

这是您的代码,相当清理:

$query = DB::table('employee')->select('employee_name', 'employee_salary', 'employee_position', 'employee_city', 'employee_extension', "employee_joining_date", 'employee_age', 'employee_id');

$filters = ['EmployeeNameSeach', 'SalarySeach', 'PositionNameSeach', 'CitySeach', 'ExtensionSeach', 'JoiningDataSeach', 'AgeSeach'];

foreach ($filters as $filter) {
    if (empty($$filter)) continue;

    $query->where('employee_name', 'LIKE', '%'.$$filter.'%');
}

$query->orderBy($columns[$column_index_for_ordering], $column_ordering_direction)
      ->skip($start_data_index)
      ->take($per_page_content_number);

$data = $query->get();

上面的代码使用$$filter作为variable variables来循环过滤器。我不知道那些过滤器的位置,但你可能应该将它们放在一个关联数组中。会让生活更轻松。