skip to Main Content

**Hello guys i just wanna know why my familyMembers variable is undefined in my view table, im using laravel.
**

here is my show method in clientController


    public function show(Client $client = null)
    {


        $familyMembers = FamilyMember::where('client_id', $client->id)->get();

        return view('layouts.social-worker.index', compact('client', 'familyMembers'));
    }

my routing

Route::get('/social-worker/{clientId}', [ClientController::class, 'show'])->name('social-worker.index')->middleware('social');

and this is my modal ay resources/views/layouts/social-worker/index.blade.php


@isset($client)
<div class="modal fade" id="nextModal{{ $client->id }}" tabindex="-1" role="dialog" aria-labelledby="nextModal{{ $client->id }}Label" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <h5 class="modal-title" id="nextModal{{ $client->id }}Label">Family Composition</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <!-- Form Action -->
            <form action="{{ route('social-worker.index', ['clientId' => $client->id]) }}" method="POST">
                @csrf
                @method('PUT')

                <!-- Modal Body -->
                <div class="modal-body">
                    <!-- Table -->
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <!-- Table Headings -->
                                    <th>Last Name</th>
                                    <th>First Name</th>
                                    <th>Middle Name</th>
                                    <th>Relationship to Client</th>
                                    <th>Birthday</th>
                                    <th>Age</th>
                                    <th>Sex</th>
                                    <th>Civil Status</th>
                                    <th>Educational Attainment</th>
                                    <th>Occupation</th>
                                    <th>Monthly Income</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody id="familyMembersTableBody">
                                @forelse($familyMembers as $familyMember)
                                <tr>
                                    <!-- Family Member Data -->
                                    <td>{{ $familyMember->fam_lastname }}</td>
                                    <td>{{ $familyMember->fam_firstname }}</td>
                                    <td>{{ $familyMember->fam_middlename }}</td>
                                    <td>{{ $familyMember->fam_relationship }}</td>
                                    <td>{{ $familyMember->fam_birthday }}</td>
                                    <td>{{ $familyMember->fam_age }}</td>
                                    <td>{{ $familyMember->fam_gender }}</td>
                                    <td>{{ $familyMember->fam_status }}</td>
                                    <td>{{ $familyMember->fam_education }}</td>
                                    <td>{{ $familyMember->fam_occupation }}</td>
                                    <td>{{ $familyMember->fam_income }}</td>
                                    <td>
                                        <!-- Edit and Delete Buttons -->
                                        <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#editFamilyMemberModal{{ $familyMember->id }}">
                                            <i class="fas fa-edit"></i> Edit
                                        </button>
                                        <form action="{{ route('social-worker.destroy', ['id' => $familyMember->id]) }}" method="POST" style="display:inline;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger btn-sm">
                                                <i class="fas fa-trash"></i> Delete
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="12" class="text-center">No family members found</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    <!-- Add Family Member Button -->
                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addFamilyMemberModal{{ $client->id }}">
                        <i class="fas fa-plus"></i> Add Family Member
                    </button>
                </div>

                <!-- Modal Footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endisset

**> I want to fetch all the data in family_members table associated based on id in clients table

but i get some error everytime i load the page.**

here is my first route wherein i need to click the edit on table

Route::get('/social-worker', [ClientController::class, 'caselist'])->name('social-worker.index')->middleware('social');

2

Answers


  1. The problem is in the controller in the way you are using the route parameter and retrieving the "Client" model please check the modifications

    public function show($clientId)
    {
        $client = Client::findOrFail($clientId);
        $familyMembers = FamilyMember::where('client_id', $client->id)->get();
    
        return view('layouts.social-worker.index', compact('client', 'familyMembers'));
    }
    
    Login or Signup to reply.
  2. Instead of using cliendId on your route, make use of Laravel’s Route Model Binding by type hinting the route to a model instance.

    From:

    Route::get('/social-worker/{clientId}', [ClientController::class, 'show']);
    

    To:

    Route::get('/social-worker/{client}', [ClientController::class, 'show']);
    

    Using this, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search