0

i have an array with values

Array
(
    [0] => Python
    [1] => PHP
    [2] => MySQL
)

So i want search these three items from tables using like query

$employees = DB::table('audit_employee_basics')
                    ->select('audit_employee_basics.id as empid', 'emp_name', 'emp_code', 'designation_name', 'emp_company_email_id', 'emp_contact_number', 'emp_gender', 'emp_location'
                            , 'department_name', 'emp_joining_date', 'fk_emp_previous_exp', 'image')
                    ->join('audit_department', 'audit_employee_basics.emp_fk_dep', '=', 'audit_department.id')
                    ->join('audit_employee_skillset', 'audit_employee_skillset.fk_emp_id', '=', 'audit_employee_basics.id')
                    ->join('audit_designation', 'audit_designation.id', '=', 'audit_employee_basics.emp_fk_des_id')
                    ->where('primary_skill ', 'like', '%' .  implode("' OR primary_skill LIKE '%", $skill_name) .'%')
                    ->distinct()
                    ->get();

I tried where('primary_skill ', 'like', '%' . implode("' OR primary_skill LIKE '%", $skill_name) .'%') which is not working for me.Any help would be highly appreciated.

3 Answers 3

1

->where('primary_skill ', 'like', '%' . implode("' OR primary_skill LIKE '%", $skill_name) .'%')

Above line is incorrect since you can't inject multiple where inside a single where condition. QueryBuilder will just take that as a string for comparison.

You can use regexp to match each value with a regular expression created from your $skill_name array as

->where('primary_skill ', 'regexp', '(' .  implode("|", $skill_name) .')')

This would get compiled as

 where primary_skill regexp '(Python|PHP|MySQL)'
Sign up to request clarification or add additional context in comments.

Comments

0

implode() is not how the way one should build such DB queries in Eloquent/Laravel.

Instead it should look like this:

...
->where(function($query) {
  $query->where('primary_skill', 'like', '%Python%');
  $query->orWhere('primary_skill', 'like', '%PHP%');
  $query->orWhere('primary_skill', 'like', '%MySQL%');
})
...

Of course, you will need to make this dynamic, e.g. using a foreach.

...
->where(function($query) {
  foreach(['Python', 'PHP', 'MySQL'] as $keyword) {
    $query->orWhere('primary_skill', 'like', "%$keyword%");
  }
})
...

Comments

0

You can do this with a foreach loop and ->orWhere.

$employees = DB::table('audit_employee_basics')
                    ->select('audit_employee_basics.id as empid', 'emp_name', 'emp_code', 'designation_name', 'emp_company_email_id', 'emp_contact_number', 'emp_gender', 'emp_location'
                            , 'department_name', 'emp_joining_date', 'fk_emp_previous_exp', 'image')
                    ->join('audit_department', 'audit_employee_basics.emp_fk_dep', '=', 'audit_department.id')
                    ->join('audit_employee_skillset', 'audit_employee_skillset.fk_emp_id', '=', 'audit_employee_basics.id')
                    ->join('audit_designation', 'audit_designation.id', '=', 'audit_employee_basics.emp_fk_des_id')
                    ->where(function ($query) use ($book, $skills_array) {
                          foreach($skills_array as $skill_name) {
                              $query->orWhere('primary_skill', 'LIKE', '%' . $skill_name . '%');
                          }
                    })
                    ->distinct()
                    ->get();

where $skills_array is your array.

Comments

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.