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 "query result count as equals to zero which means $count=0". Previously when I wrote code for the first time it has worked fine but now its not working.
My form is :
<form id="my_form_id" 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 MySql Table Structure & data is: lead_audit
Structure:
ColumnName DataType Length
id int 11
supplier_id int 11
source varchar 255
client_id int 11
campaign_id varchar 255
data text
received datetime
errors blob
disposition varchar 255
id supplier_id source client_id campaign_id data received errors disposition
1 6 6 1 56 --- 2017-07-07 11:19:25 False Accepted
1 6 6 1 56 --- 2017-07-07 11:19:25 False Accepted
My Query is below:
if(isset($request->suppliers) && $request->suppliers == 'suppliers'){
$results->leftjoin('suppliers AS s', 's.id', '=', 'l.supplier_id')
->addSelect('s.name AS Supplier')
->groupBy('s.name')
->orderBy('s.name');
}
if(isset($request->source) && $request->source == 'source'){
$results->addSelect('l.source AS Source')
->groupBy('l.source')
->orderBy('l.source');
//$results->leftjoin('source AS so', 'so.id', '=', 'l.source_id')
}
if(isset($request->campaign) && $request->campaign == 'campaign'){
$results->leftjoin('campaigns AS c', 'c.id', '=', 'l.campaign_id')
->addSelect('c.name AS Campaign')
->groupBy('c.name')
->orderBy('c.name');
}
if(isset($request->clients) && $request->clients == 'clients'){
$results->leftjoin('clients AS cl', 'cl.id', '=', 'l.client_id')
->addSelect('cl.name AS Client')
->groupBy('cl.name')
->orderBy('cl.name');
}
$queryResults = $results->addSelect('l.disposition AS Disposition')
->selectRaw('COUNT(*) as Count')
->whereBetween('l.received', [$start, $end])
->groupBy('l.disposition')
->orderBy('l.disposition')
->get()->toArray();
$count = count($queryResults);
echo "Count: ".$count;exit;
if($count > 0){
$results = array_map(function($item) {
return (array)$item;
}, $queryResults);
return Excel::create('my_excel_name', function($excel) use ($results) {
$excel->sheet('sheet_name', function($sheet) use ($results) {
$sheet->fromArray($results);
});
})->setFilename($filename)
->download('csv');
} else{
\Session::flash('error','No data has been found.');
return redirect('my_controller/create');
}
Resulting query is:
array:1 [▼
0 => array:3 [▼
"query" => "select `s`.`name` as `Supplier`, `l`.`source` as `Source`, `c`.`name` as `Campaign`, `cl`.`name` as `Client`, `l`.`disposition` as `Disposition`, COUNT(*) as Count from `lead_audit` as `l` left join `suppliers` as `s` on `s`.`id` = `l`.`supplier_id` left join `campaigns` as `c` on `c`.`id` = `l`.`campaign_id` left join `clients` as `cl` on `cl`.`id` = `l`.`client_id` where `l`.`received` between ? and ? group by `s`.`name`, `l`.`source`, `c`.`name`, `cl`.`name`, `l`.`disposition` order by `s`.`name` asc, `l`.`source` asc, `c`.`name` asc, `cl`.`name` asc, `l`.`disposition` asc ◀"
"bindings" => array:2 [▼
0 => "2017-07-01"
1 => "2017-07-08"
]
"time" => 149.01
]
]
If I print below:
print_r($queryResults);
echo "Count: ".$count;exit;
then o/p
Array ( ) Count: 0
Why Am I getting $count value always '0'. What's wrong in my code?
It is too late to post an answer but I hope this may help someone. The mistake which I made is in giving start date and end date formats like below:
Problem:
$start = "2017-07-01"
$end = "2017-07-08"
But Actually I have to give the input like below:
Solution:
$start = "2017-07-01 00:00:00"
$end = "2017-07-08 00:00:00"
My problem is solved after adding time as "00:00:00" to start and end dates. Thanks everyone.