! have three table
I have use laravel yajra datatable
. i need sum and search of starting_balance this field in inventory_has_warehouses pivot table
my code:
$id = Auth::user()->id;
$row = Inventory::with('contact')->with('warehouse')
->select(
'inventories.*',
DB::raw('SUM(inventory_has_warehouses.starting_balance) as total')
)
->leftJoin('inventory_has_warehouses', 'inventory_has_warehouses.inventory_id', '=', 'inventories.id')
->leftJoin('warehouses', 'warehouses.id', '=', 'inventory_has_warehouses.warehouse_id')
->where('inventories.subscriber_id',$id)
->groupBy('inventories.id');
$datatable = DataTables::of($row)
->filterColumn('total', function($query, $keyword) {
$query->whereRaw('sum(inventory_has_warehouses.starting_balance) like ?', ['%'.$keyword.'%']);
})
return $datatable->make(true);
but i fount this type of error
Exception Message:↵↵SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select count() as aggregate from (select
inventories
., SUM(inventory_has_warehouses.starting_balance) as total frominventories
left joininventory_has_warehouses
oninventory_has_warehouses
.inventory_id
=inventories
.id
left joinwarehouses
onwarehouses
.id
=inventory_has_warehouses
.warehouse_id
whereinventories
.subscriber_id
= 2 andinventories
.status
= 1 and (LOWER(inventories
.itemcode
) LIKE %1% or LOWER(inventories
.purchasedescription
) LIKE %1% or exists (select * fromcontacts
whereinventories
.supplier
=contacts
.id
and LOWER(contacts
.name
) LIKE %1%) or (sum(inventory_has_warehouses.starting_balance) like %1%)) group byinventories
.id
) count_row_table)
mysql query
select inventories., SUM(inventory_has_warehouses.starting_balance) as total from inventories left join inventory_has_warehouses on inventory_has_warehouses.inventory_id = inventories.id left join warehouses on warehouses.id = inventory_has_warehouses.warehouse_id where inventories.subscriber_id = 2 and inventories.status = 1 and (LOWER(inventories.itemcode) LIKE %1% or LOWER(inventories.purchasedescription) LIKE %1% or exists (select * from contacts where inventories.supplier = contacts.id and LOWER(contacts.name) LIKE %1%) or (sum(inventory_has_warehouses.starting_balance) like %1%)) group by inventories.id
$id = Auth::user()->id;
$row = DB::table('inventories')->select('inventories.*','contacts.name',DB::raw('SUM(inventory_has_warehouses.starting_balance) as total'))
->leftJoin('contacts', 'inventories.supplier', '=', 'contacts.id')
->leftJoin('inventory_has_warehouses', 'inventories.id', '=', 'inventory_has_warehouses.inventory_id')
->where('inventories.subscriber_id',$id)
->groupBy('inventory_has_warehouses.inventory_id');
if ($keyword = $request->get('search')['value']) {
$row->having(DB::raw('SUM(inventory_has_warehouses.starting_balance)'), 'like', '%'.$keyword.'%');
$row->orHaving('inventories.itemcode', 'like', '%'.$keyword.'%');
$row->orHaving('inventories.purchasedescription', 'like', '%'.$keyword.'%');
$row->orHaving('contacts.name', 'like', '%'.$keyword.'%');
}
$datatable = DataTables::of($row)
->filterColumn('total', function($query, $keyword) {
})
return $datatable->make(true);