Laravel子查询上的参数编号无效

时间:2019-01-15 16:12:03

标签: laravel

运行以下查询时,出现以下错误:无效的参数编号:混合的命名和位置参数。

float32

如果我将$subQuery = DB::table('earliest_count') ->select('reporting_week') ->where('vendor_name', $vendorName); $dates = DB::table('invoice') ->select('week_beginning_date', 'week_end_date') ->whereRaw(':sql BETWEEN `week_beginning_date` AND `week_end_date`', [':sql' => DB::raw("({$subQuery->toSql()})")]) ->where('week_beginning_date', '<', $date) ->orderBy('week_beginning_date') ->limit(1) ->mergeBindings($subQuery) ->get(); 替换为以下内容,它将起作用:

whereRaw

如何使子查询正常工作而不必将确切的查询写为字符串?

修改

我确实尝试了以下操作,但没有错误,但没有任何结果。 (当我以字符串形式输入子查询时,确实会得到结果):

->whereRaw('(SELECT reporting_week FROM earliest_count WHERE vendor_name = "My Vendor") BETWEEN `week_beginning_date` AND `week_end_date`')

1 个答案:

答案 0 :(得分:0)

Laravel不使用命名的占位符,您应该使用?作为占位符,并从参数数组中删除该名称。

赞:

->whereRaw('? BETWEEN `week_beginning_date` AND `week_end_date`', [DB::raw("({$subQuery->toSql()})"])