Laravel 查询生成器 - 其中 (1=1) 出现错误

Laravel Query Builder - where(1=1) getting error

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

$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);

有人可以帮帮我吗? 谢谢

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

Column not found: 1054 Unknown column '1' in 'where clause'

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

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

生成的 SQL 将是:

WHERE `1` = 1

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

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

不需要 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 来遍历过滤器。我不知道这些过滤器来自哪里,但您可能应该将它们放在关联数组中。会让生活更轻松。