Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 为什么要“得到”;array#u map():参数“2应为数组”;运行复杂查询时出错?_Mysql_Search_Filter_Laravel 5.4_Laravel Query Builder - Fatal编程技术网

Mysql 为什么要“得到”;array#u map():参数“2应为数组”;运行复杂查询时出错?

Mysql 为什么要“得到”;array#u map():参数“2应为数组”;运行复杂查询时出错?,mysql,search,filter,laravel-5.4,laravel-query-builder,Mysql,Search,Filter,Laravel 5.4,Laravel Query Builder,我正在为过滤器搜索编写代码,其中用户从3个复选框列表中选择至少一个选项,然后我必须为所选选项(复选框)编写查询。如果我试图运行我的代码,那么我会得到“array_map():Argument#2应该是一个数组” 我的表格是: <form id="statistics" method="post" action="{{ action('MyController@store') }}""> <div class="form-group"> <label><

我正在为过滤器搜索编写代码,其中用户从3个复选框列表中选择至少一个选项,然后我必须为所选选项(复选框)编写查询。如果我试图运行我的代码,那么我会得到“array_map():Argument#2应该是一个数组”

我的表格是:

<form id="statistics" method="post" action="{{ action('MyController@store') }}"">
<div class="form-group">
 <label><input type="checkbox" value="suppliers" name="suppliers">suppliers</label>
</div>
<div class="form-group">
 <label><input type="checkbox" value="campaign" name="campaign">campaign</label>
</div>
<div class="form-group">
 <label><input type="checkbox" value="clients" name="clients">clients</label>
</div>
<div class="form-group">
 <label for="start_date">From:</label><input type="text" name="startdate" id="startdate">
</div>
<div class="form-group">
 <label for="start_date">From:</label><input type="text" name="enddate" id="enddate">
</div>
<div class="form-group">
 <button type="submit" id="process" class="btn btn-primary submit">Run Report</button>
</div>
如果我运行上面的代码,那么我会得到ann错误,如“array#u map():Argument#2应该是一个数组”

它在正常查询中运行良好,如下所示:

$results = DB::table('lead_audit AS l')
         ->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
         ->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
         ->select('s.name AS Supplier', 'c.name AS Campaign', 'l.disposition AS Disposition')
         ->whereBetween('l.received', [$start, $end])
         ->groupBy('s.name', 'c.name', 'l.disposition')
         ->orderBy('s.name')
         ->orderBy('c.name')
         ->orderBy('l.disposition')
         ->get()->toArray();
$count = count($results);

谁能帮我整理一下吗,谢谢。

看起来$results只是一个查询而不是数组。您需要更改为下面的代码

$queryResults = $results->whereBetween('l.received', [$start, $end])
    ->groupBy('l.disposition')         
    ->orderBy('l.disposition')
    ->get()->toArray();
$count = count($results);
if($count > 0)
{
 $results = array_map(function ($value) {
            return (array)$value;
        }, $results); 
下面的代码

$results->whereBetween('l.received', [$start, $end])
    ->groupBy('l.disposition')         
    ->orderBy('l.disposition')
    ->get()->toArray();
$count = count($results);
if($count > 0)
{
 $results = array_map(function ($value) {
            return (array)$value;
        },$queryResults);

在上面的代码中,我将查询结果分配给变量$queryResults,然后将其传递到数组映射函数中。

谢谢@PhongTran的工作非常出色。我是laravel 5.4的新手,所以我面临一些问题。这是我的一个很小的错误,但我已经为此浪费了一天。我想知道像我这样的开发人员是否也面临这种类型的问题。。!!!无论如何,非常感谢。干杯。@prasadchinthala我在开始使用Laravel时也遇到了这个问题,我的建议是在得到预期结果时使用dd()进行调试。谢谢,仅供参考。感谢Stackoverflow,这是一个非常适合开发人员的博客。乐于助人。快乐编码@PhongTran
$results->whereBetween('l.received', [$start, $end])
    ->groupBy('l.disposition')         
    ->orderBy('l.disposition')
    ->get()->toArray();
$count = count($results);
if($count > 0)
{
 $results = array_map(function ($value) {
            return (array)$value;
        },$queryResults);