laravel querybuilder 如何在 where 函数中使用 like

2021-12-18 00:00:00 php laravel-5 laravel-query-builder

$book = array('book1','book2');$book 数组元素编号是可变的.它可能有 2 个元素或 20 个元素
我需要进行这样的查询:

$book = array('book1','book2'); $book array elements numbers are variable. it might have 2 element or 20 elements
I need to make a query like this:

select * from book where bookname like %book1% or bookname like %book2%

要在 Laravel 5 中进行此查询,有一个选项:

To make this query in laravel 5 there is an option :

$name = DB::Table('bookinfo')
          ->select('*')
          ->wherein('bookname',$book)
          ->get();

但它使用 = 运算符我需要使用 like 运算符

but it use = operator I need to use like operator

推荐答案

感谢大家帮助我,但我通过这样做解决了:

Thanks everyone for helping me but i solved it by doing:

$book = array('book2','book3','book5');  

$name = DB::Table('bookinfo')
        ->select('BookName', 'bookId')                
        ->Where(function ($query) use($book) {
             for ($i = 0; $i < count($book); $i++){
                $query->orwhere('bookname', 'like',  '%' . $book[$i] .'%');
             }      
        })->get();

相关文章