为什么在运行复杂查询时得到“array_map():参数#2应该是一个数组”错误?

时间:2017-06-06 18:56:51

标签: mysql search filter laravel-5.4 laravel-query-builder

我正在为过滤搜索编写代码,其中用户从3个复选框的列表中选择至少一个选项,然后我必须为所选选项(复选框)编写查询。如果我尝试运行我的代码然后我得到“array_map():参数#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>

我的查询如下:

if($request->suppliers == 'suppliers'){
        $results->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
                ->select('s.name AS Supplier')
                ->groupBy('s.name')
                ->orderBy('s.name');
    }
if($request->campaign == 'campaign'){
        $results->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
                ->select('c.name AS Campaign')
                ->groupBy('c.name')
                ->orderBy('c.name');
    }
if($request->clients == 'clients'){
        $results->leftjoin('clients AS cl', 'cl.id', '=', 'l.client_id')
                ->groupBy('cl.name')
                ->orderBy('cl.name');
    }
$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); 
 Excel::create('Statistics', function($excel) use($results) {
            $excel->sheet('Statistics', function($sheet) use($results) {
                $sheet->fromArray($results);
            });
        })->download('csv');
    }

如果我运行上面的代码然后我得到像“array_map()那样的错误:参数#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);

任何人都可以帮忙解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:0)

看起来$ 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 ,然后将其传递给 array_map 函数。