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

AbdulBazith's avatar

Problem with adding same id values in view.blade file

Guys i have two tables, income_type and income

income_type consist of fields

id
income_type

and income table consist of fields

id
hotel_id
income_date
income_type_id(fk income_type table)
income_amount

so i have three hotels entry in these tables.

now i need to add the total of three hotels for each income_type id.

that is parcel income_type for hotel1 is 300, for hotel2 is 400 means then parcel - 700 like this i need,

so what i did is

used groupby

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


 $incomes = [];

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

  {

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

   $total_income = 0;

   foreach ($incomes as $item) {


    $total_income += $item;

   }


  }



if i give dd($incomes); means it shows

array:6 [▼
  "ROOM SERVICE" => 29
  "ROOM BILL" => 36
  "BAGS INCOME" => 580
  "PARCEL" => 85
  "TIFFIN" => 120
  "A/C SALES" => 500
]

here how this output came is ROOM SERVICE => 29 means hotel1 room service 10+hotel2 roomservice 10 + hotel3 roomservice 9 .

like this all the other also came.

and why i used this means the below

 $total_income = 0;

   foreach ($incomes as $item) {


    $total_income += $item;

   }

the dd( $total_income); 1350 the total of all the above.

now whats the problem iam facing is how to display this is the view file.

with name and amount.

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');
    }

in my blade file what i did is

 <tbody>
                                        <tr>
                                            <td colspan="4" align="left"><b>INCOME SECTION</b></td>
                                        <tr>

                                            @foreach($incomes as $income)
                                        <tr>

                                            <td width="20%">{{ $income->income_type->income_type }}</td>
                                            <td width="20%">{{ $income->income_amount }}</td>
                                            <td></td>
                                            <td></td>
                                        </tr>

                                        @endforeach
                                        <tr>
                                            <td colspan="1" align="center">
                                                <b> Total Income Value </b>
                                            </td>
                                            <td>
                                                <b> {{ $total_income }} </b>
                                            </td>
                                            <td colspan="2"></td>

                                        </tr>
                                    </tbody>



this showing error

Trying to get property 'income_type' of non-object (View:   

how can i display the name and the amount.

Kindly some one help.

0 likes
8 replies
Vandan29's avatar

Change Your income_type model

public function income()

{

      return $this->hasMany('App\Income');

}

Try this

Snapey's avatar

This is really messy and impenetrable.

OK, so what if for one income type, there is no income.

This will break <td width="20%">{{ $income->income_type->income_type }}</td>

anywhere you have x->y->z you should allow for y not being present. An easy way is to use the null coalesce operator;

<td width="20%">{{ $income->income_type->income_type ?? '---' }}</td>
AbdulBazith's avatar

@snapey thank you for you response.

i used like this


                                        <tr>

                                            <td width="20%">{{ $income->income_type->income_type  ?? '---' }}</td>
                                            <td width="20%">{{ $income->income_amount  ?? '---' }}</td>
                                            <td></td>
                                            <td></td>
                                        </tr>

the out put is refer: https://imgur.com/bmoD1aP

why it became all dashes. why the particulars are not displayed. whats the problem. the amount also not displayed. but i have these...

i need the name of income types particulars

if i give dd($incomes); means it shows

array:6 [▼
  "ROOM SERVICE" => 29
  "ROOM BILL" => 36
  "BAGS INCOME" => 580
  "PARCEL" => 85
  "TIFFIN" => 120
  "A/C SALES" => 500
]



the above mentioned are the particulars.

in the image i show you.

in the particulars column the name ROOM SERVICE and so on must be displayed,

and in the Income column 29 the amount must be displayed.

Expecting like this

Particulars             Income

ROOM SERVICE            29
ROOM BILL               36
BAGS INCOME         580
PARCEL                  85
TIFFIN                  120
A/C SALES               500


Total                   1350

How could i get this???

Snapey's avatar
Snapey
Best Answer
Level 122

with a simple array like this

array:6 [▼
  "ROOM SERVICE" => 29
  "ROOM BILL" => 36
  "BAGS INCOME" => 580
  "PARCEL" => 85
  "TIFFIN" => 120
  "A/C SALES" => 500
]

then use as $key=>value

<tbody>
    <tr>
        <td colspan="4" align="left"><b>INCOME SECTION</b></td>
    </tr>

    @foreach($incomes as $key => $value)
        <tr>
            <td width="20%">{{ $key }}</td>
            <td width="20%">{{ $value }}</td>
            <td></td>
            <td></td>
        </tr>
    @endforeach

    <tr>
        <td colspan="1" align="center">
            <b> Total Income Value </b>
        </td>
        <td>
            <b> {{ $total_income }} </b>
        </td>
        <td colspan="2"></td>
    </tr>
</tbody>

Please or to participate in this conversation.