laravel querybuilder how to use like in wherein function

Al-Alamin picture Al-Alamin · Dec 17, 2015 · Viewed 23.8k times · Source

$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%

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

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

but it use = operator I need to use like operator

Answer

Al-Alamin picture Al-Alamin · Dec 17, 2015

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();