如何使用 yajra 数据表 laravel 5.7 中的 laravel 集合输出可点击的删除按钮

How to output a clickable delete button using laravel collectives in yajra datatables laravel 5.7

我想使用 laravel 集合在 yajra 数据表的另一列上输出一个可点击的删除按钮。问题是它在视图中输出原始 html 文本 {!! Form::open(["action" => ["UsersController@destroy",28], "method" => "POST", "class" => "pull-right"]) !!} {{ Form::hidden("_method", "DELETE") }} {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }} {!! Form::close()!!}") 而不是可点击的删除按钮。我可以输出常规 html 标签,但我想使用 laravel 集合。我很困惑为什么即使我已经添加了 rawColumns() 函数它也不起作用。

这是我的控制器:

public function yajraDT()
{
    $users = User::all();
    return Datatables::of($users)
    ->addColumn('delete', function ($users) {
            return '{!! Form::open(["action" => ["UsersController@destroy",'.$users->id.'], "method" => "POST", "class" => "pull-right"]) !!}
            {{ Form::hidden("_method", "DELETE") }}
            {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }}
            {!! Form::close()!!}")';
    })
    ->rawColumns(['delete'])
    ->make(true);
}

这是我的观点:

<script>
     $(function() {
           $('#tableDT').DataTable({
           processing: true,
           serverSide: true,
           ajax: '{{ url('users/yajraDT') }}',
           columns: [
                    { data: 'id', name: 'id', 
                        @if(Auth::check() && Auth::user()->type == "Admin")
                            render:function(data, type, row)
                            {
                                return "<a href='/users/"+ row.id +"'>" + row.id + "</a>"
                            }
                        @endif},
                    { data: 'first_name', name: 'first_name' },
                    { data: 'last_name', name: 'last_name' },
                    { data: 'email', name: 'email' },
                    { data: 'gender', name: 'gender' },
                    {data: 'delete', name: 'delete'}
                 ]
        });
     });
</script>

我是这样做的:

// in the controller

return Datatables::of($users)
    ->addColumn('delete', function ( $user ) {
        return view('user.delete', compact('user'))->render();
    })
    ->escapeColumns([])
    ->make();

那么你的delete.blade.php

{!! Form::open(["action" => ["UsersController@destroy", $user->id], "method" => "POST", "class" => "pull-right"]) !!}
    {{ Form::hidden("_method", "DELETE") }}
    {{ Form::submit("Delete", ["class" => "btn btn-danger"]) }}
{!! Form::close()!!}

这样您就不会在控制器中进行硬编码 HTML,您可以在显示之前渲染 html 输出。

另一个建议是将您的 Admin 检查从 JS 代码移到您的控制器中,您可以在 Datatable 上使用 editColumn 方法。

我认为 laravel collective 是用于 blade 的,所以不需要把它放在控制器上。简单地:-

public function yajraDT()
{
    $users = User::all();
    return Datatables::of($users)
    ->addColumn('delete', function (User $users) {
            return '<a href="admin/user/'.$users->id.'></a>';
    })
    ->rawColumns(['delete'])
    ->make(true);
}

//ROUTE Start
Route::get('admin/user/delete/{id},'Users@destroy');
//ROUTE End

//Remove User
public function destroy($id){
 try{
      User::where('id',$id)->delete();
      return back()->with([
      'message'  => 'User removed successfully."
     ]);
    }catch(Exception $e){
       return back()->with([
          'message' => $e->getMesssage() 
       ]);
    }
  }