Some one reply please
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.
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.