my table
table1
|table1_id | name |
| 1 john |
| 2 dave |
| 3 carl |
table2
|table2_id| table1_id| type |status
| 1 | 1 | shoes |paid
| 2 | 1 | bag |paid
| 3 | 2 | bag |paid
| 4 | 2 | shoes |unpaid
table3
|table3_id|table2_id|item |amount|
|1 | 1 |nike |1000 |
|2 | 1 |adidas |2000 |
|3 | 2 |lv |1000 |
|4 | 3 |lv1 |2000 |
|5 | 3 |lv |1000 |
|6 | 4 |adidas |1000 |
this is the result I want to display john --- total paid shoes and bag dave <-- total of bag,'shoes is unpaid so 1000 is not added to total'
|name|total |
|john|4000 |
|dave|3000 |
|carl|0 |
this is my controller it gives me an error the total is same in all name
public function index()
{
$fetch = DB::table('table1')
->leftjoin('table2','table1.table1_id','=','table2.table1_id')
->leftjoin('table3','table2.table2_id','=','table3.table2_id')
->select('table1.*','table2.*',DB::raw('(select sum(table3.amount) from table3
join table2 on table2.table2_id = table3.table2_id
where table.status = "paid") as used'))
->groupBy('table1.table1_id')
->get();
return $fetch;
}