如何写sql'喜欢' Laravel的陈述?

时间:2017-08-04 14:39:57

标签: php laravel laravel-5 laravel-query-builder

您将如何用Laravel样式编写以下查询?

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

我在下面尝试了一些东西,但它不起作用:

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

2 个答案:

答案 0 :(得分:3)

试试这个,它应该有效:

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

答案 1 :(得分:0)

像这样:

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

在你的情况下尝试这样:

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

参考:laravel where clauses