如何在Laravel中写sql 'like'语句?

How to write sql 'like' statement in Laravel?

您将如何以 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)

像这样:

$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

试试这个,它应该有效:

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