You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LaraAdmin模块中添加删除确认功能?

Adding Delete Confirmation to LaraAdmin Modules

Hey there! Let's fix that missing delete confirmation prompt for your LaraAdmin modules—no more accidental record deletions! I’ve worked with LaraAdmin before, so here are two straightforward approaches to get this sorted:

Option 1: Add Confirmation to a Single Module

If you only need this feature for one specific module, tweak its list view file directly:

  1. Navigate to your module’s index view: resources/views/la/<your-module-name>/index.blade.php
  2. Locate the line that renders the delete button. It’ll look something like this:
    {!! la_btn('delete', 'Delete', 'btn-danger', route('la.<your-module-name>.destroy', $row->id)) !!}
    
  3. Add an onclick attribute to trigger a confirmation prompt:
    {!! la_btn('delete', 'Delete', 'btn-danger', route('la.<your-module-name>.destroy', $row->id), ['onclick' => "return confirm('Are you sure you want to delete this record? This action can\'t be undone!')"]) !!}
    
    Replace <your-module-name> with the actual name of your module (e.g., users for a User module) and adjust the confirmation message to your preference.

Option 2: Global Confirmation for All Modules

Want this safety net across every module without repeating work? Add a global JavaScript listener:

  1. Open LaraAdmin’s global script partial (typically resources/views/la/layouts/partials/scripts.blade.php)
  2. Paste this snippet at the end of the file:
    document.addEventListener('DOMContentLoaded', function() {
        // Target all LaraAdmin delete buttons
        document.querySelectorAll('.btn-danger[href*="destroy"]').forEach(button => {
            button.addEventListener('click', function(e) {
                if (!confirm('Are you sure you want to delete this record? This action can\'t be undone!')) {
                    e.preventDefault(); // Stop the delete request if user cancels
                }
            });
        });
    
        // Optional: Handle form-based delete submissions (if your setup uses them)
        document.querySelectorAll('form[action*="destroy"]').forEach(form => {
            form.addEventListener('submit', function(e) {
                if (!confirm('Are you sure you want to delete this record? This action can\'t be undone!')) {
                    e.preventDefault();
                }
            });
        });
    });
    
  3. Save the file—now every delete button across all modules will show a confirmation prompt before proceeding!

Quick Tips

  • If your LaraAdmin setup uses AJAX for deletions (common in custom builds), adjust the JS to hook into the AJAX request trigger instead of the click event.
  • Feel free to customize the confirmation message to match your app’s tone or add extra context for users.

内容的提问来源于stack exchange,提问作者Sandeep Yadav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:00:15