简体   繁体   English

来自原始查询的laravel查询生成器

[英]laravel query builder from raw query

Good day! 美好的一天! I'am new to Laravel. 我是Laravel的新手。 I tried many ways to do it but still it gives me an error. 我尝试了很多方法,但它仍然给我一个错误。 I'm trying to convert this sql query into eloquent 我正在尝试将此sql查询转换为eloquent

 Select     t.employee_code, 
            CASE WHEN t.day = '2017-08-19' THEN t.PRESENT ELSE NULL END AS `2017-08-19`,
            CASE WHEN t.day = '2017-08-20' THEN t.PRESENT ELSE NULL END AS `2017-08-20`,
            CASE WHEN t.day = '2017-08-21' THEN t.PRESENT ELSE NULL END AS `2017-08-21`,
            CASE WHEN t.day = '2017-08-22' THEN t.PRESENT ELSE NULL END AS `2017-08-22`,
            CASE WHEN t.day = '2017-08-23' THEN t.PRESENT ELSE NULL END AS `2017-08-23`,
            CASE WHEN t.day = '2017-08-24' THEN t.PRESENT ELSE NULL END AS `2017-08-24`
FROM (
select e.employee_code, 
    Cast(e_l.time_in As date) As Day,
    Case 
        WHEN e_l.time_in IS NULL THEN 'A' 
        WHEN DAYOFWEEK(e_l.time_in) In(7, 1) Then 'W'
        ELSE 'P' 
    end as PRESENT
from employee As e
left join employees_logs As e_l on e.id = e_l.employee_id)
AS t

And I tried this eloquent way 我尝试了这种雄辩的方式

public static function statusReport($data){
$start_date = $data['start_date'];
$end_date = $data['end_date'];
$date_array = self::getDatesFromRange($start_date, $end_date);

$query = DB::raw("(Select t.employee_code,
          CASE WHEN t.day = '$start_date' THEN t.Present ELSE NULL END AS '$start_date')");
$query->addSelect(
        DB::raw("(
            SELECT employee.id as emp_id, 
            CONCAT(employee.firstname, " ",employee.lastname) AS employee_name,
            CAST(employees_log.time_in as date) as date_given,
            CASE    WHEN employee_logs.time_in IS NULL THEN 'A'
                    WHEN leave.status_id = 4 AND leave_request.with_pay = 1  THEN 'L'
                    WHEN leave.status_id = 4 AND leave_request.with_pay = 0  THEN 'LOP'
                    WHEN DAYOFWEEK(employee_logs.time) In(7, 1) THEN 1 AS 'W' 
                    ELSE 'P' END as status
            LEFT JOIN employee_logs On employee.id = employees_logs.employee_id
            JOIN leave On employee.id = leave.emp_id
            JOIN leave_request On leave.id = leave_request.leave_id
            WHERE employee_logs.time_in BETWEEN '$start_date' AND '$end_date') As `t`"));
$data = $query->get();
return $data;       

} }

这是我得到的错误

the expected output is this query will tell the status of employee in each day. 预期的输出是这个查询将告诉每天员工的状态。 For example if this day is Weekend it will show 'W' on the output. 例如,如果这一天是周末,它将在输出上显示“W”。 if the employee is Absent 'A' and if present 'P' . 如果员工缺席'A'并且如果出现'P'。 I hope someone can help me with this. 我希望有人可以帮助我。 Thanks in advance 提前致谢

At first take a look at the addSelect method: 首先看一下addSelect方法:

public function addSelect($column)
{
    $column = is_array($column) ? $column : func_get_args();
    $this->columns = array_merge((array) $this->columns, $column);
    return $this;
}

This just adds the column(s) by merging with existing selected columns. 这只是通过与现有选定列合并来添加列。

For more clarification visit https://laravel.com/docs/5.4/queries#selects 有关更多说明,请访问https://laravel.com/docs/5.4/queries#selects

Now I guess you will solve your problem without using addSelect method. 现在我猜你会在不使用addSelect方法的情况下解决你的问题。

My Reference this 我参考了这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM