I don't understand your question, give us the input data , the sql you try, the result and the expected result.
Dec 30, 2020
13
Level 4
How to Add Sql query in collection
I want to to add this sql to my DB:: SELECT COUNT(slave_id) FROM salesWHEREslave_id!=0 GROUP BYbill_number`
I tried to add like this but when i run does not return any collection data, because i want to count this column slave_id
$data=DB::table('sales')
->select('users.name as nametag','customers.*','teams.*','locations.*','customer__orders.*','sales.*','devices.*',\DB::raw("group_concat(devices2.Devicenumber SEPARATOR '\n') as slavename"),'sales.created_at as created_at_sale',\DB::raw('(SELECT COUNT(`slave_id`) FROM `sales` WHERE `slave_id` !=0 GROUP BY `bill_number`) as totalslave'))
->leftJoin('customers','customers.id','=','sales.client_id')
->leftJoin('teams','teams.id','=','sales.tag_id')
->leftJoin('locations','locations.id','=','sales.borderName')
->leftJoin('devices','devices.id','=','sales.unit_issue_id')
->leftJoin('users','users.id','=','sales.user_id')
->leftJoin('customer__orders','customer__orders.id','=','sales.TruckNo_id')
->leftJoin("devices as devices2",\DB::raw("FIND_IN_SET(devices2.id,sales.slave_id)"),">",\DB::raw("'0'"))
->where([
['sales.sale_type',2],
['sales.tag_id',$team]
])
->groupBy('sales.bill_number')->get();
Please or to participate in this conversation.