0

How would you write the following query in Laravel style?

SELECT * FROM `job_details` WHERE `job_title` LIKE '%officer%' AND `category_id` = 1 AND `city_id` = 1

I tried something below but it does not work:

DB::(job_details)->where(job_title LIKE '%officer%')->and(category_id=1)->and(city_id=1)

2 Answers 2

7

Try this, it should work:

DB::table('job_details')->where('job_title', 'like', '%officer%')
                      ->where('category_id', 1)
                      ->where('city_id', 1)
                      ->get();
Sign up to request clarification or add additional context in comments.

6 Comments

I am trying to fetch the data but it is showing blank page..this is my code:
$check = DB::table('job_details')->where('job_title', 'like', '%$keyword%') ->where('category_id', $category_id) ->where('city_id', $city_id)->get(); foreach($check as $result) { echo $result->job_title."<br>"; }
What's the result of dd($check)? Probably it is empty.
yes it shows emply..but i have jobs with title 'officer'..it is supposed to show me the result
Do it like this where('job_title', 'like', '%'.$title.'%') ;)
|
1

Like this :

$users = DB::table('users')
                ->where('name', 'like', 'T%')
                ->get();

in your case try like this :

  DB::table('job_details')
          ->where([
            ['job_title', 'like', '%officer%'],
            ['category_id', '=', 1], 
            ['city_id', '=', 1]
                 ])->get();

refer : laravel where clauses

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.