Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbdulBazith's avatar

How to get total based on income_type group with different id.

Guys i am working with a project Inventory control like system

The project maintains the whole purchase, income, expenses of a hotel.

the hotel is of three branches. so to make this application used by all the three hotels, i kept separate login for everything.

consider that three hotels S1, S2, and S3

the hotel calculates their income at the end of the day.

so what i did is,

had a form to enter the income_type like

ROOM SERVICE

ROOM BILL

MEALS

PARCEL

TIFFEN

the table income_types contains the fields

id,
login_user_id,
income_type,
created_at,
updated_at

this is the data in that table

id                      login_user_id                   income_type 
1                           1                       ROOM SERVICE
2                           1                       ROOM BILL
3                           1                       MEALS
4                           2                       ROOM SERVICE
5                           2                       ROOM BILL
6                           2                       MEALS

where

login_user_id   1 =S1 hotel
              2= S2 hotel                           


and this is the incomes table with columns

id,
login_user_id,
income_date,
income_type_id,
income_amount,


and the data are

id  login_user_id       income_date         income_type_id      income_amount
1   1               6-04-19             1                   200
2   1               6-04-19             2                   100
3   1               6-04-19             3                   50  
4   2               6-04-19             4                   600
5   2               6-04-19             5                   700
6   2               6-04-19             6                   800 

Now as i said each shop has separate login id and password

so if S1 is login, that user can take that day income report.

what he gets for the date 6-04-19 is

ROOM SERVICE -  200

ROOM BILL  -    100

MEALS    -      50


and for the second branch S2

ROOM SERVICE -  600

ROOM BILL  -    700

MEALS    -      800

this above given are separate income report for one day for two shops S1 and S2

now i need to get both the shops report with total of income_type.

what i expect is

ROOM SERVICE -  800   // (600+200)

ROOM BILL  -    800  //(700+100)

MEALS    -      850 // (800+50)

this is what the output i expect.

but whats the problem is the login user id are different so that the id's of the income_type also different.

now what can i do??

is there any solution please kindly someone help please.

i think it is possible when the income_type id's are same.

But due to confusion i have given all the details separetely for the users.

i used these codes to get the total values based on income_type

//this fetches all income values based on login id
 $incomes=Income::where ('login_user_id',auth()->id())->get();


//declared a array for income type
 $income_type=[];


// Initialized the $income_type with   0
 foreach ($incomes as $key => $income)
 {
           $income_type[$income->income_type_id]["total_amount"] = 0;
         }

// add the total amount based in the income_type

         foreach ($incomes as $key => $income) 
{
             $income_type[$income->income_type_id]["total_amount"] += $income->income_amount;
}




when i dd($income_type)

the result is


array:4 [▼
  1 => array:1 [▼
    "total_amount" => 200
  ]
  2 => array:1 [▼
    "total_amount" => 100
  ]
  4 => array:1 [▼
    "total_amount" => 600
  ]
  5 => array:1 [▼
    "total_amount" => 700
  ]
]



All the above code worked for same id's for another concept in another project.

but here the id's are different so the value are not added what can i do

i thought an idea that,

id's are different but the names will be same.. so why cant i try with income_type names?

bot in controller how can i get the names of income_type with this relation.

this is my income_type model

 public function income()
    {
        return $this->hasMany('App\Income', 'income_type_id');
    }


this is my income model

 public function income_type()
    {
        return $this->belongsTo('App\IncomeType', 'income_type_id');
    }

if i need the name of income type in my view.blade file just i use

{{$income->income_type->income_type}}

but in controller how can i use the name of the income_type??

Kindly some one help please

0 likes
5 replies
mungpara's avatar

If you need all hotel data then you should not apply where condition for user on income_types table.

try this code and let me know it work or not.

$data = Income::with('income_type')->get()->groupBy('income_type_id');

$income_type = [];

foreach ($data as $key => $item)
{
    $income_type[$item[0]['income_type']['income_type']]= collect($item)->sum('income_amount');
}
AbdulBazith's avatar

@mungpara thank you so much for you response.

when i tried your code

it first showed error Parse error: syntax error, unexpected ';', expecting ']'

so i changed to

   $data = Income::where('income_date',$request->search_date)->with('income_type')->get()->groupBy('income_type_id');



$income_type = [];

foreach ($data as $key => $item)
{

    //just added one square bracket 

    $income_type[$item[0]['income_type']['income_type']] = collect($item)->sum('income_amount');


}

and in my db

the records are totally 4 records in the given date

ROOM SERVICE: 1000

ROOM BILL : 2000

ROOM SERVICE: 40

ROOM BILL : 20

when dd($income_type);


array:2 [▼
  "ROOM SERVICE" => 40
  "ROOM BILL" => 20
]

it just picking the second set of records only. but all the four records are in same date. then why it not added?? it left the first two records 1000 and 2000.just picked 40 and 20 only

why??

munazzil's avatar

Try as like below have sure with your relationship query?

       $incomes=Income::where ('login_user_id','=',auth()->id())->get();
mungpara's avatar

@ABDULBAZITH - query is ok but make sure you have data of ROOM SERVICE and ROOM BIIL.

try below code and let me know result.

$data = Income::where('income_date',$request->search_date)->get()->groupBy('income_type_id');
AbdulBazith's avatar

@mungpara thank you for your response.

sorry stuck in some other problem with authentication.

after clearing that insha allah surely i will try and i will inform u

1 like

Please or to participate in this conversation.