Skip to content

Instantly share code, notes, and snippets.

@Neeraj1005
Created July 14, 2022 08:07
Show Gist options
  • Save Neeraj1005/0e45195854513b22ee350971f6f01179 to your computer and use it in GitHub Desktop.
Save Neeraj1005/0e45195854513b22ee350971f6f01179 to your computer and use it in GitHub Desktop.
add delete functionality in same page

#in blade file to perform delete using anchor tag with js

<x-layouts.app>
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <div class="row">
                        <div class="col-md-5">
                            Employees
                        </div>
                        <div class="col-md-7">
                            <a href="{{ route('employees.create') }}"
                                class="btn btn-primary float-end">Add Employe</a>
                        </div>
                    </div>
                </div>

                <div class="card-body table-responsive p-0">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($employees as $emp)
                                <tr>
                                    <td>{{ optional($emp)->full_name }}</td>
                                    <td>
                                        <a class="btn btn-link" href="#" type="submit" role="button" onclick="event.preventDefault();
                                                        if(confirm('Are you sure!')){
                                                            document.getElementById('form-delete-'+{{ $emp->id }}).submit();
                                                        }
                                                    ">
                                            {{ __('Delete') }}
                                        </a>
                                        <form style="display:none" id="form-delete-{{ $emp->id }}"
                                            action="{{ route('employees.destroy',$emp->id) }}"
                                            method="POST">
                                            @csrf
                                            @method('delete')
                                        </form> 
                                    </td>
                                </tr>
                            @empty
                                <tr><td>No record found</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</x-layouts.app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment