SPL3S
8 months ago
126
12
Laravel

Hide submit form when data present in DB & show update form

Posted 8 months ago by SPL3S

Hey guys and girls,

I've been breaking my head for a few days trying to solve simple bug for advanced minds.

I have a simple form that submits some data to DB and it then changes to update method form. I made it work when there are no other entries in the table, but when there are more entries, my new submission form is not showing up for other users, and I know why, because of my count(). How could I change my logic instead of using count()?

As soon I enter one of the entries, @if($event->clubInfos->count()) will keep showing update form, but there are no $clubInfo->user_id == $user_auth_id present so it wont display that data with form

I tried


@if(in_array($user_auth_id, $clubInfo_user_id_array)
    @foreach($event->clubInfos as $clubInfo)
             @if($clubInfo->user_id == $user_auth_id && $event->id == $clubInfo->event_id)

but then it doesn't show "Submit" form as the same user has its ID present in another event with the clubInfo data.


    <table class="table">
        <thead>
            <tr>
                <th scope="col">
                    Club Name!!
                </th>
                <th scope="col">
                    Country
                </th>
                <th scope="col">
                    City
                </th>
                <th scope="col">
                    Head Of delegation
                </th>
            </tr>
        </thead>
            @if($event->clubInfos->count())
            @foreach($event->clubInfos as $clubInfo)
            @if($clubInfo->user_id == $user_auth_id)
        <tbody>
            
            <tr>
                <form action="/club-info/{{ $clubInfo->id }}" method="POST">
                    @method('PATCH')
                @csrf
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_name" required="" type="text" value="{{ $clubInfo->club_name }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_country" required="" type="text" value="{{ $clubInfo->club_country }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_city" required="" type="text" value="{{ $clubInfo->club_city }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_head_of_delegation" required="" type="text" value="{{ $clubInfo->club_head_of_delegation }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <button class="btn btn-success" style="cursor:pointer" type="submit">
                                Update
                            </button>
                        </div>
                    </td>
                </form>
                <td>
                    <form action="/club-info/{{ $clubInfo->id }}" method="POST">
                        @method('DELETE')
                        @csrf
                        <button class="btn btn-warning" style="cursor:pointer" type="submit">
                            clear
                        </button>
                    </form>
                </td>
            </tr>
        </tbody>
        @endif
        @endforeach
        @else
        <tbody>
            <form action="/events/{{ $event->id }}/club-info" method="POST">
                @csrf
                <tr>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_name" required="" type="text" value="{{ old('club_country') }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_country" required="" type="text" value="{{ old('club_country') }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_city" required="" type="text" value="{{ old('club_city') }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <input class="form-control" name="club_head_of_delegation" required="" type="text" value="{{ old('club_head_of_delegation') }}">
                            </input>
                        </div>
                    </td>
                    <td>
                        <div class="form-group">
                            <button class="btn btn-primary" style="cursor:pointer" type="submit">
                                Save
                            </button>
                        </div>
                    </td>
                </tr>
            </form>
        </tbody>
    @endif
    </table>
</div>

Please sign in or create an account to participate in this conversation.