I have referral commission details and different levels of referral users I need to show that in the table with there are levels. Here I did coded but I'm not satisfied with the logic please let me know the logic is correct or wrong you see the below images for reference.
What i'm looking for

Here my table

What i did so far
Controller
public function inDirectCommission()
{
$user = User::where('id', 1)->first();
$user_id = $user->id;
$direct_commissions = Referral::where('parent_id', $user_id)->get();
foreach ($direct_commissions as $row) {
$second_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($second_level as $row) {
$third_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($third_level as $row) {
$forth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($forth_level as $row) {
$fifth_level = Referral::where('parent_id', $row->user_id)->get();
foreach ($fifth_level as $row) {
}
}
}
}
}
return view('site.user.dashboard.commission.in_direct_commission', compact('second_level', 'third_level', 'forth_level', 'fifth_level'));
}
Blade
<table id="example1"
class="table table-bordered table-striped dataTable dtr-inline direct-table"
role="grid" aria-describedby="example1_info">
<thead>
<tr>
<th>Date</th>
<th>Referal</th>
<th>Level</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
@foreach ($second_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>2<sup>nd</sup> Level</td>
<td>4000</td>
</tr>
@endforeach
@foreach ($third_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>3rd<sup>rd</sup> Level</td>
<td>3000</td>
</tr>
@endforeach
@foreach ($forth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>4<sup>th</sup> Level</td>
<td>2000</td>
</tr>
@endforeach
@foreach ($fifth_level as $row)
<tr>
<td>{{ $row->created_at }}</td>
<td>{{ $row->user->name }}</td>
<td>5<sup>th</sup> Level</td>
<td>1000</td>
</tr>
@endforeach
</tbody>
</table>
dd of second level user
