Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

convert sql query to laravel eloquent

//Ref: https://jjlabajo.github.io/SQLtoEloquent/

SELECT emp.*, dep.department_name, al.emp_id, al.auth_date, min(al.auth_time) as 'check_in', 
max(al.auth_time) as 'check_out' FROM employees as emp
LEFT JOIN attendance_log as al ON emp.device_emp_id = al.emp_id
JOIN departments as dep ON emp.department_id = dep.id
WHERE al.emp_id IN $emp_id   // $emp_id is an array of employee's ID
WHERE date(al.auth_date) BETWEEN $start_date AND $end_date
GROUP BY emp.device_emp_id, date(al.auth_date_time)

After Convert:
DB::table("employees as emp")
->leftJoin("attendance_log as al", function($join){
	$join->on("emp.device_emp_id", "=", "al.emp_id");
})
->join("departments as dep", function($join){
	$join->on("emp.department_id", "=", "dep.id");
})
->select("emp.*", "dep.department_name", "al.emp_id", "al.auth_date", "min (al.auth_time) as 'check_in'", "max (al.auth_time) as 'check_out'")
->whereIn("al.emp_id", "$emp_id")
->groupBy("emp")
->get();     
             
Source by jjlabajo.github.io #
 
PREVIOUS NEXT
Tagged: #convert #sql #query #laravel #eloquent
ADD COMMENT
Topic
Name
9+3 =