Count and Display Results as 1 of 2 in Laravel 8 in an existing foreach Loop

Viewed 186

I have an existing foreach loop that fetches details and displays the results in a table. For example, I want to add a count of how many people assigned to that record have edited and submitted it back. For example

ID Number Edits 

1  CI-123  1 of 2

2  CI-456  0 of 3

I have tables tracking these edits, and when creating a record, a record is saved to a request table. When editing and submitting a record, it gets saved in a different table.

@foreach($records as $confirm)
    <tr>
        <td class ="text-center"></td>
        <td class ="text-center"><a href="{{ route('info.record.show', $confirm->number) }}">{{ $confirm->number}}</a></td>
        <td class ="text-center">{{ $confirm->client_name}}</td>
        <td class ="text-center">{{ $confirm->user_name }}</td>
        <td class ="text-center">{{ $confirm->user_id }}</td>
        <td class ="text-center">{{ $confirm->description }}</td>
        <td class ="text-center">{{ $confirm->instructions }}</td>
        <td class ="text-center">{{ $editedy->count() }} of {{ $editedall->count() }}</td>
    </tr>
@endforeach

The problem with this is it only counts the top record and duplicates it to all the other records. So if it's 1 of 2, all of them, display 1 of 2. Any ideas? $records, $editedy and $editedall are select queries.

$records = DB::table('documents')
    ->join('users', 'users.id', '=', 'documents.user_id')
    ->join('clients', 'clients.id', '=', 'documents.client_id')
    ->where('documents.is_edited', 1)
    ->where('documents.user_id', $id)
    ->get();

$editedall = DB::table('edited_requests')
    ->where('edited_requests.requestor_id', $id)
    ->where('edited_requests.document_id', $confirms->pluck('id'))
    ->get();

$editedy = DB::table('edit_requests')
    ->join('documents', 'documents.id', '=', 'edit_requests.document_id')
    ->where('edit_requests.requestor', $id)
    ->where('edit_requests.signed', 'Yes')
    ->get();

return view('info.record.index', compact('confirms', 'editedy', 'editedall'));
0 Answers
Related