数据表中的按钮删除 laravel 8

button remove in datatable laravel 8

我正在尝试通过一个按钮从我的数据库中删除一个数据。我正在做一个表格

public function datatable()
{
    $data = User::all();
    
    return Datatables::of($data)
        ->addColumn('action', function($user){

            $btn = '<div class="row justify-content-center align-items-center">
                        <a href='.route('options.users.show', $user->id).' class="btn btn-warning btn-sm m-1">
                            <i class="fas fa-edit"></i>
                        </a>';
                $btn .= '<form action="{{ route(\'options.users\','.$user->id.') }}" method="POST">
                        '.csrf_field().''.method_field("DELETE").'
                            <button type="submit" class="btn btn-danger btn-sm"
                            onclick="return confirm(\'Are You Sure Want to Delete?\')"><i class="fas fa-trash"></i></a>
                        </form> 
                    </div>';
                    
            return $btn;
        })
        ->rawColumns(['action'])
        ->make(true);
}

但是当点击我的按钮时 return 404 错误和我的 URL 是这个....

options/%7B%7B%20route('options.users',1)%20%7D%7D

我不知道我做错了

/** 更新 */

Route::group(['prefix'=>'options','as'=>'options.'], function(){
    Route::get('/opciones', 'OpcionesSistema@index')->name('index');
    Route::post('/cambiarFotoPerfil', 'OpcionesSistema@changeImages')->name('cambiarFotoPerfil');
    Route::post('/cambiarDatosLogin', 'OpcionesSistema@changeLoginData')->name('cambiarDatosLogin');
    Route::post('/cambiarNombreSistema', 'OpcionesSistema@changeNameSystem')->name('changeNameSystem');
    Route::resource('roles', 'RolesPermisos');
    Route::get('users/datatable', 'UserController@datatable')->name('datatable');
    Route::resource('users', 'UserController');
    
});

/** 更新 2 */

控制器:

/**
     * FUNCTION TO RETURN DATATABLE
     */
    public function datatable()
    {
        $data = User::all();
        
        return Datatables::of($data)
            ->addColumn('action', function($user){
                return view('layouts.datatableActions', ['user' => $user]);
            })
            ->rawColumns(['action'])
            ->make(true);
    }

layouts/view

@props('user')
    <div class="row">
        <a href='{{ route('options.users.show', $user) }}' class="btn btn-warning btn-sm m-1">
            <i class="fas fa-edit"></i>
        </a>

        <form action="{{ route('options.users', $user) }}" method="POST">
            @method('DELETE')
            @csrf
            <input type="submit" />
        </form>
    </div>

我的新错误是:消息:\n\nUndefined variable $attributes (View: C:\xampp\htdocs\clinica\resources\views\layouts\datatableActions.blade.php)"

您位于 PHP 块内,这意味着 Blade 语法将不起作用。只需删除它,然后连接路由,就像您拥有其余的 PHP 函数一样,就像它之前的那一行一样。

$btn .= '<form action="'.route('options.users',$user->id).'" method="POST">
                    '.csrf_field().''.method_field("DELETE").'
                        <button type="submit" class="btn btn-danger btn-sm"
                        onclick="return confirm(\'Are You Sure Want to Delete?\')"><i class="fas fa-trash"></i></a>
                    </form> 
                </div>';

试试这个:

你的view.blade.php

@props(['user'])

<form action="{{ route('options.users', $user) }}" method="POST">
    @method('DELETE')
    @csrf
    <input type="submit" />
</form>

您的控制器:

public function datatable()
{
    $data = User::all();
    
    return Datatables::of($data)
        ->addColumn('action', function($user){
            return view('view', ['user' => $user]);
        });

在你的 web.php:

Route::delete('users', 'UserController');