Just a wild guess, but since you're grouping by project id, shouldn't you also resolve that in your call? Meaning:
$item->project_id->client->name
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I keep coming up agains this problem it doesn't make any sense as the data I am requesting has data and is nested in a collection, I solved it with another instance by using find(1) in the query but that was for one result with multiple relationships i.e. user->projects.
The following code returns a json object with the correct data.
ReportsController.php
public function report(Request $request)
{
$time = explode(" - ", $request->input('time'));
$client_id = $request->input('client_id');
$start = $this->change_date_format($time[0]);
$end = $this->change_date_format($time[1]);
$entries = TimesheetEntry::with('client','project','timesheetTask','timesheet')
->where('client_id', $client_id)
->whereBetween('timesheet_entries.updated_at', [$start, $end])
->get();
$items = collect($entries)->groupBy('project_id');
return $items;
}
The thing is, is that when I try to render the view with blade I get the ErrorException (Property [client] does not exist on this collection instance.)
report.blade.php
@foreach($items as $item)
<tr>
<td>{{ $item->client->name }}</td>
<td>{{ $item->project->name }}</td>
</tr>
@endforeach
I have searched everywhere for a definitive answer but nothing even comes close to explaining it. I have read Adam Wathans book on collections and no mention of this issue.
your help would be so welcome at this point.
many thanks
Mark
I am not sure this the 'right way' to do it, but it works none the less
The controller
$time = explode(" - ", $request->input('time'));
$client_id = $request->input('client_id');
$start = $this->change_date_format($time[0]);
$end = $this->change_date_format($time[1]);
$entries = TimesheetEntry::with('project')
->whereHas('project', function($q) use ($client_id){
$q->where('client_id', '=', $client_id);
})
->whereBetween('updated_at', [$start, $end])
->get()->groupBy('project_id');
return view('_modules.admin.reports.report', compact(
'entries',
'start',
'end'
));
and the view
@foreach($entries as $item)
<tr>
<td>{{ $item[0]->project->client->name }}</td>
<td>{{ $item[0]->project->project_name }}</td>
<td>{{ $item->sum('mon_hrs') + $item->sum('tue_hrs') + $item->sum('wed_hrs') + $item->sum('thu_hrs') + $item->sum('fri_hrs') + $item->sum('sat_hrs') + $item->sum('sun_hrs') }}</td>
</tr>
@endforeach
It took some thinking, and lots of research, but then what doesn't.
I think the docs need to include some blade examples of collection rendering as there is very little out there for this subject.
anyway, this is my best shot so far
Please or to participate in this conversation.