2015-09-26 166 views
1

我有一個查詢,在PMA中正常工作,我試圖將其轉換爲雄辯,但我必須缺少一些東西。 這裏的SQL查詢:laravel 5.1雄辯查詢

SELECT t1.* FROM ppr__child_absence t1 
LEFT JOIN ppr__children t3 ON t3.idcode=t1.child_idcode 
WHERE t1.id = (SELECT t2.id FROM ppr__child_absence t2 
    WHERE t2.child_idcode = t1.child_idcode 
    ORDER BY t2.id DESC LIMIT 1) 
AND t3.deleted_at IS NULL 
AND $input BETWEEN start AND stop; 

和HES是我的口才查詢:

$input = Request::all(); 
$pprs = 
    DB::table('ppr__child_absence') 
    ->join('ppr__children', function($join) { 
     $join->on('ppr__children.idcode', '=', 'ppr_children_absence.child_idcode') 
      ->where('ppr__child_absence.child_idcode', '=', DB::raw('SELECT t2.id FROM ppr__child_absence t2 
       WHERE t2.child_idcode = ppr__child_absence.child_idcode 
       ORDER BY t2.id DESC LIMIT 1'));}) 
      ->whereNull('ppr__children.deleted_at') 
      ->whereBetween($input, array('ppr_children_absence.start','ppr_children_absence.stop'))->get(); 

我不斷收到錯誤: ErrorException在Grammar.php線58: 用strtolower()預計參數1字符串,數組給定。

這裏有人能讓我走向正確的方向嗎? 基本上用戶有1日期輸入與日期選擇器,並提交按鈕來獲得結果根據選擇的日期。

工作查詢這裏出錯誤,但沒有結果

$input = Request::all(); 
    $pprs = DB::table('ppr__child_absence') 
      ->select('ppr__child_absence.*') 
      ->join('ppr__children', function($join) { 
      $join->on('ppr__children.idcode', '=', 'ppr__child_absence.child_idcode') 
       ->where('ppr__child_absence.id', '=', DB::raw('SELECT t2.id FROM ppr__child_absence t2 
        WHERE t2.child_idcode = ppr__child_absence.child_idcode 
        ORDER BY t2.id DESC LIMIT 1'));}) 
       ->whereNull('ppr__children.deleted_at') 
       ->where('ppr__child_absence.start', '<', $input['ppr_day']) 
       ->where('ppr__child_absence.stop', '>', $input['ppr_day'])->get(); 

回答

1

$input = Request::all();存儲陣列中的所有輸入字段。

您正在使用->whereBetween($input,...),並且whereBetween()的第一個參數預計爲字符串(表列的名稱),但您傳遞的是數組。

您沒有提供有關輸入的詳細信息。假設輸入包含一個稱爲'字段'的字段,則應該使用->whereBetween($input['field'],...)而不是

+0

謝謝!我確實想念那個,但也有別的。我收到錯誤SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'2015-09-26'。它試圖找到一列,並不認爲它是一個日期。即使它在模型文件中聲明。 $輸入字段字段名稱將是'ppr_day'。 – Merrant

+0

還在DB :: table('ppr__child_absence')後添加了一行 - > select('ppr__child_absence。*') – Merrant

0

您似乎想要的不是whereBetween方法提供的內容。當使用whereBetween時,邏輯是您希望給定的列值在兩個輸入值之間。在Laravel Query Builder Documentation的例子選擇得好:

// the column 'votes' value should be between 1 and 100 
$users = DB::table('users')->whereBetween('votes', [1, 100]); 

你似乎需要的是確保輸入值是兩個不同的列值之間。你可以做到這一點有兩個獨立的條件:

->where('ppr_children_absence.start', '<', $input['value']) 
->where('ppr_children_absence.stop', '>', $input['value']) 

這將確保$input['value'])startstop列值之間。

+0

謝謝!這個+小錯字修正了這個伎倆。我看起來像它的工作,不給出錯誤,但結果是0即使我在數據庫中獲得一些數據。 – Merrant

+0

查詢轉儲給了我這個 ' 「SELECT'ppr__child_absence'。* FROM'ppr__child_absence' \t \t 內加入'ppr__children'上'ppr__children'.'idcode' = 'ppr__child_absence'.'child_idcode' \t \t和'ppr__child_absence'.'id' =? \t \t其中'ppr__children'.'deleted_at'爲空 \t \t和'ppr__child_absence'.'start' <?和 'ppr__child_absence'。'stop' >?「 ' 看起來像原始查詢不執行或什麼?爲什麼它是」?「? – Merrant

+0

嘗試使用'whereRaw'而不是'where',就像這樣'' - whereRaw('t1.id =(SELECT t2.id FROM ppr__child_absence t2 WHERE t2.child_idcode = t1.child_idcode ORDER BY t2.id DESC LIMIT 1)')'。 – Bogdan