2015-09-26 185 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