简体   繁体   English

laravel 5.1雄辩的查询

[英]laravel 5.1 eloquent query

I got a query that works fine in PMA and I'm trying to convert it to Eloquent, but I must be missing something. 我有一个在PMA中工作正常的查询,并且我试图将其转换为Eloquent,但我一定缺少一些东西。 Here the SQL query: 这里是SQL查询:

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;

and hes is my eloquent query: 嘿是我雄辩的查询:

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

I'm keep getting error: ErrorException in Grammar.php line 58: strtolower() expects parameter 1 to be string, array given. 我不断收到错误消息:Grammar.php第58行中的ErrorException:strtolower()期望参数1为字符串,给定数组。

Can anyone here lead me to right direction? 这里有人可以引导我朝正确的方向前进吗? Basically user has 1 date input with datepicker and submit button to get result according to the date chosen. 基本上,用户使用日期选择器输入1个日期,然后根据选定的日期提交按钮以获取结果。

working query here with out errors but no results 工作查询在这里没有错误,但没有结果

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

$input = Request::all(); stores all the input fields in an array. 将所有输入字段存储在一个数组中。

You are using ->whereBetween($input,...) and the first parameter of whereBetween() is expected to be a string (the name of the table column) but you are passing an array. 您正在使用->whereBetween($input,...)并且whereBetween()的第一个参数应该是字符串(表列的名称),但是您正在传递数组。

You didn't give details about the input. 您没有提供有关输入的详细信息。 Assuming the input contains a field called 'field', the you should use ->whereBetween($input['field'],...) instead 假设输入包含一个名为“ field”的字段,则应使用->whereBetween($input['field'],...)

What you seem to want is not what the whereBetween method offers. 您似乎想要的不是whereBetween方法提供的内容。 When using whereBetween the logic is that you want a given column value to be between two input values. 使用whereBetween ,逻辑是希望给定的列值介于两个输入值之间。 The example in the Laravel Query Builder Documentation is chosen well: Laravel查询生成器文档中的示例选择得当:

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

What you seem to want is to make sure an input value is between two different column values. 您似乎想要确保输入值在两个不同的列值之间。 You can do that by having two separate conditions: 您可以通过具有两个单独的条件来做到这一点:

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

This will make sure that $input['value']) is between the start and stop column values. 这将确保$input['value'])startstop列值之间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM