2014-08-27 152 views
3

我正試圖在Laravel中執行whereIN查詢。我想匹配的值是數據庫表中的值,如:「a,b,c,d」如何在Laravel中執行whereIN查詢?

我想選擇上面包含在此字符串中的所有用戶名。 所以我把字符串和爆炸它創建一個數組,然後我將這個數組添加到Laravel命令。問題是,它返回只有一個用戶只匹配字母「a」例如,它停止。

的控制器代碼:

public function returnEorti(){ 

    $month = Input::get('month'); 
    $day = Input::get('day'); 

    $eorti = DB::select("select text from days where day='$day' and month='$month'"); 
    $eortiFinal = explode(",", $eorti[0]->text); 
    $pelatis = Pelatis::whereIn('Onoma', $eortiFinal)->get(); 

    return json_encode($pelatis); 
} 

阿賈克斯:

$.ajax({ 
    type:"GET", 
    url:"getEorti", 
    data:{"day":d[2],"month":d[1]}, 
    success:function(data){ 
     var b = jQuery.parseJSON(data) 
     for(var i=0;i<Object.keys(b).length;i++){ 
      $("#test").append(b[i].Onoma+'</br>'); 
     }  
     console.log(data);  
    }    
}); 

我的路線:

Route::get('getEorti','[email protected]'); 

的 'Pelates' 表: 的p_id Onoma Epitheto Onoma_Miteras Onoma_Patera Hmerominia_Gennisis Tilefono_Oikias Kinito_Tilefono Diefthinsi DIMOS Periferia Periferiaki_Enotita Epaggelma 電子郵件

+0

'$ eortiFinal'中的實際值是什麼,以及您期望匹配哪個'Pelatis' db行?這不完全是[SSCCE](http://sscce.org/) – dehrg 2014-08-27 16:40:58

+0

Pelatis是客戶的表... $ eortiFinal是一個數組,它有「a,b,c,d」我之前提到但被''分隔符分解。 – sarakinos 2014-08-27 16:53:30

+0

是的,我意識到Pel​​atis是一張桌子,桌子上有什麼行? – dehrg 2014-08-27 16:56:01

回答

0

存在DB查詢

$eorti = DB::select("select text from days where day='$day' and month='$month'");

一個語法錯誤,應更正爲

eorti = DB::table('days')->whereDay($day)->whereMonth($month)->get(); 
+0

不幸的是,它不幫助...問題是,查詢返回只有用戶匹配字符串中的第一個元素,並忽略其餘部分。 – sarakinos 2014-09-02 11:42:44

+0

例如:我有兩個名字爲'Maria','Panagiotis'的用戶。當查詢開始時,它獲取日期並找到當天名稱的字符串....例如,找到的字符串是'Maria ,'Panagiotis','Marios'。用戶返回的只是'Maria' – sarakinos 2014-09-02 11:43:09

+1

您是否嘗試僅返回'$ pelatis'或'$ pelatis-> toJson()'而不是json_encode($ pelatis)? – tharumax 2014-09-03 22:37:17