0

Is there any way to convert following query to laravel query builder?

select `employee_id`
from `otc_employee_qualifications` 
where `emp_qualifctn_type` IN ('29','27') 
group by `employee_id`
having count(Distinct `emp_qualifctn_type`) = 2

enter image description here

4
  • do u need to know how to write query for laravel or you want to write code to automatically create query for laravel from given query?? Commented Nov 20, 2015 at 9:10
  • i want to just convert that query to laravel query builder. thats all. Commented Nov 20, 2015 at 9:12
  • you can have an idea ...visit laravel.com/docs/5.0/queries Commented Nov 20, 2015 at 9:13
  • What you have tried so far? Post your attempts. If you simply tried to search Laravel Query Builder then you simply get the docs too Commented Nov 20, 2015 at 9:14

2 Answers 2

3

Try as below :

$users = DB::table('otc_employee_qualifications')
         ->select('employee_id')
         ->whereIn('emp_qualifctn_type', [27,29])
         ->groupBy('employee_id')
         ->having(DB::raw("count(Distinct emp_qualifctn_type)"), '=', 2)
         ->get();
Sign up to request clarification or add additional context in comments.

11 Comments

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'employee_id,DB::raw("distinct' in 'field list' (SQL: select employee_id,DB::raw("distinct as as from otc_employee_qualifications where emp_qualifctn_type in (27, 29) group by employee_id having counter = 2)
Convert ->select('employee_id,DB::raw("distinct emp_qualifctn_type as counter')) into ->select('employee_id',DB::raw("distinct emp_qualifctn_type as counter"))
You're Welcome. @AG21
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct emp_qualifctn_type as counter from otc_employee_qualifications where ' at line 1 (SQL: select employee_id, distinct emp_qualifctn_type as counter from otc_employee_qualifications where emp_qualifctn_type in (27, 29) group by employee_id having emp_qualifctn_type = 2)
@JishadP please check your having clause
|
-2

Answer:

  DB::select('employee_id')
        ->from('otc_employee_qualifications')
        ->whereIn('emp_qualifctn_type', ('29', '27'))
        ->groupBy('employee_id')
        ->having(DB::raw('count(Distinct emp_qualifctn_type)'), '=', 2)
        ->get();

You can convert SQL query into laravel eloquent query by using bellow website.

This will convert SQL query to laravel eloquent base query

Convert SQL query to Eloquent base query

1 Comment

how to convert, this query to laravel "select * from officer_availabilities where officer_id = 52 and 1 BETWEEN start_day AND end_day"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.