2015-12-17 13 views
7

$ book = array ('book1', 'book2'); $ पुस्तक सरणी तत्व संख्याएं चरणीय हैं। यह 2 तत्व या 20 तत्वों हो सकता है
मैं इस तरह एक प्रश्न बनाने की जरूरत है:लार्वाल क्वेरीबिल्डर कैसे उपयोग करें जिसमें फ़ंक्शन

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

laravel वहाँ 5 में इस क्वेरी बनाने के लिए एक विकल्प है:

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

लेकिन यह = ऑपरेटर का उपयोग मैं एक स्तंभ के रूप में 1 या n तत्वों का उपयोग अपने बयान के साथ like ऑपरेटर

उत्तर

10

सभी को धन्यवाद मेरी मदद करना लेकिन मैंने इसे हल करके हल किया:

$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(); 
+0

यह @ सोना लाइफ के उत्तर के बराबर है, सिवाय इसके कि आपका और अधिक सुरुचिपूर्ण है। –

-3
$name = DB::Table('bookinfo') 
     ->select('*') 
     ->wherein('bookname','like','%'.$book.'%') 
     ->get(); 
+0

$ पुस्तक एक सरणी है। आप इसे इस तरह से संयोजित नहीं कर सकते हैं। – Leif

+0

मैंने कोशिश की लेकिन यह त्रुटि दिखा दी: TestController.php लाइन में त्रुटि अपवाद 116: स्ट्रिंग रूपांतरण –

2

उपयोग करने के लिए एक गतिशील प्रश्न के लिए की जरूरत है रूपांतर: के लिए आप एक कच्चे बयान उपयोग कर सकते हैं "की तरह":

$collection = DB::Table('bookinfo')->select('*'); 
foreach($book as $key => $element) { 
    if($key == 0) { 
     $collection->where(DB::raw('bookname like %'.$element.'%')); 
    } 
    $collection->orWhere(DB::raw('bookname like %'.$element.'%')); 
} 
$name = $collection->get(); 

http://laravel.com/docs/5.1/queries

Raw Expressions

या आप इस तरह से इसका इस्तेमाल कर सकते हैं: के लिए

$collection = DB::Table('bookinfo')->select('*'); 
foreach($book as $key => $element) { 
    if($key == 0) { 
     $collection->where('bookname', 'like', '%'.$element.'%'); 
    } 
    $collection->orWhere('bookname', 'like', '%'.$element.'%'); 
} 
$name = $collection->get(); 
+0

$ पुस्तक सरणी तत्व संख्या परिवर्तनीय हैं। इसमें 2 तत्व या 20 तत्व –

+0

ठीक हो सकते हैं, मैंने अपना जवाब अपडेट कर दिया है। – goldlife

+0

thanx यह काम करता है –

-1
$name = DB::Table('bookinfo') 
     ->select('*') 
     ->where("bookname like '%book1%' OR bookname like '%book2%'") 
     ->get(); 
+1

$ पुस्तक सरणी तत्व संख्या चरणीय हैं। इसमें 2 तत्व या 20 तत्व हो सकते हैं –

0
$name = DB::Table('bookinfo') 
      ->select('*') 
      ->where('bookname','LIKE','%'.$book[0].'%') 
      ->orWhere('bookname','LIKE','%'.$book[1].'%') 
      ->get(); 
संबंधित मुद्दे