I have Clients
and ClientsLimitCategory
table, i want joining these table using leftJoin
in Laravel and counting when i am grouping thats value.
I have working MySQL query like this:
SELECT A.* , COUNT(*) AS TotalClient FROM `ClientsLimitCategory` A
LEFT JOIN `Clients` B ON B.AgreementType = A.CategoryName
WHERE A.CategoryName ='GOLD'
GROUP BY A.CategoryName
And using this code in Laravel :
$keyword = (!empty(isset($arrSearch['keyword'])) && $arrSearch['keyword'] != "undefined") ? $arrSearch['keyword'] : '';
$active = (!empty(isset($arrSearch['active'])) && $arrSearch['active'] != "undefined") ? $arrSearch['active'] : '';
// $type = (!empty(isset($arrSearch['type'])) && $arrSearch['type'] != "undefined") ? $arrSearch['type'] : '';
$sql = DB::table($this->table)
->leftJoin('Clients', 'AgreementType','=','ClientsLimitCategory.CategoryName')
->select('ClientsLimitCategory.*',DB::raw('count(*) as TotalClient'))
->where(function($query) use ($keyword){
$query->where('CategoryName', 'like', '%' .$keyword. '%')
->orWhere('CategoryDescription','like','%'.$keyword.'%');
});
$sql = (!empty(isset($active)) && $active != "") ? $sql->where('active','=',$active) : $sql;
$sql = (!empty(isset($type)) && $type != "") ? $sql->where('type','=',$type) : $sql;
$sql = $sql->groupBy('ClientsLimitCategory.CategoryName');
// // ->get();
$sql = $sql->orderBy('ClientsLimitCategory.CreatedDate','DESC');
$hasil['count'] = $sql->count();
$sql = $sql->paginate($limit);
$hasil['data'] = $sql;
$hasil['prinQuery'] = DB::getQueryLog();
But i got this error: