phpmysqllaravel-5filterlaravel-query-builder

why getting "array_map(): Argument #2 should be an array" error while running complex query?


I am writing code for filter search where user selects atleast one option from a list of 3 checkboxes then I have to write a query for the selected options(checkboxes). If I try to run my code then I am getting "array_map(): Argument #2 should be an array".

My form is :

<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>

My query is below:

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');
    }

If I run the above code then I am getting ann error like "array_map(): Argument #2 should be an array"

It was working fine With normal query like below:

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

Can anyone please helpme how to sort out this, Thanks.


Solution

  • Look like $results is just a query not an array. You need to change to code below

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

    to following code

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

    In the above code, I assign result of the query to variable $queryResults, then pass it into array_map function.