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

jinal's avatar
Level 2

htmlentities() expects parameter 1 to be string, object given

Controller: public function index() { $check = \DB::table('balance_fees')->select(DB::raw('sum(balance_fees) as total_fees')) ->groupBy ('student_id') ->get(); $semester = Semesters::lists('semester','id')->all(); $branch = Semesters::lists('branch')->all(); $students = Students::all(); // return $check; return view('students.index', compact('semester','branch','students','check')); }

view: @extends('layouts.header')

@section('content')

ID Full Name Mobile Number Semester Branch Total Fees Fees Paid Balance Fees
    </tr>
</thead>
<tbody>  @foreach($check as $check)
    <tr>
       <td>{{ $check }}</td>
       
    </tr> @endforeach  </tbody>

@stop

0 likes
5 replies
vipin93's avatar

first use ``` so can your code readable and rty to debug dd($check)

WebKenth's avatar

Back to bootcamp with you!

When in a foreach loop you cannot instantiate a variable with the same name as the collection you are trying to access, otherwise the entire collection will be overwritten on the first run.

instead write it like this

@foreach($check as $item)
    {{ $item}}
@endforeach

if your item is an object do it like this

@foreach($check as $item)
    {{ $item->property }}
@endforeach

with property being the method/attribute you are trying to access

To figure out what you have in your array you could run a dd($check) in your controller

1 like
MaverickChan's avatar

you foreach loop has been put in wrong position in a table , that should like blow

<table>
    <tr>
        @foreach($items as $item)
        <td>{{ $item }}</td>
    </tr>
        @endforeach
</table>

Please or to participate in this conversation.