Home > Enterprise >  HOW TO MAKE DELETE IN DATABALES YAJRA laravel 8
HOW TO MAKE DELETE IN DATABALES YAJRA laravel 8

Time:10-05

how to make delete in yajra datatables this my index controller

 public function index(Request $request)
    {
        // $list_user = User::all();
        if($request->ajax()){
            $user = User::latest()->get();
            return datatables()->of($user)
                    ->addIndexColumn()
                    ->addColumn('action', function (User $user){
                        $actionBtn = '<a href="'.route('user.edit',$user->id).'" title="Edit User" class="btn btn-sm" style="color: #fff;background-color: #3DCB3A;border-color: #8ad3d3"> <i class="fa fa-edit"></i> </a> 
                        <a href="'.route('user.destroy',$user->id).'" class="btn btn-danger btn-sm" title="Hapus User" onclick="hapusUsers('.$user->id.')"><i class="fa fa-trash "></i></a>';
                        return $actionBtn;
                    })
                    ->rawColumns(['action'])
                    ->make(true);
            }
        return view('dashboard.user.user');
        // return response()->json($list_user);
    }
}

and this is my destroy controller

public function destroy($id)
    {
        if($id==1){return redirect()->back();}
        $user = User::findOrfail($id);
        $user->delete();
        return response()->json(['messege'=>'success'],200);
    }

and this javascript in my view

@push('javascripts')
<script type="text/javascript">
   $(function () {
    
    var table = $('.yajra-datatable').DataTable({
          processing  : true,
          serverSide : true,
          ajax :"{{route('user.index')}}",  
          columns: [
                {data: 'DT_RowIndex', name: 'DT_RowIndex'},
                {data: 'name', name:'name'},
                {data: 'username', name:'username'},
                {data: 'email', name:'email'},
                {data: 'action', name: 'action', orderable: false, searchable: false},
                  ],
        });
    } );
</script>
@endpush

i just want make delete but i dont know to do it in controller can you fix my controller

CodePudding user response:

Replace your controller with this and check:

public function index(Request $request)
    {
        // $list_user = User::all();
        if($request->ajax()){
            $user = User::latest()->get();
            return datatables()->of($user)
                    ->addIndexColumn()
                    ->addColumn('action', function (User $user){
                        $actionBtn = '<a href="'.route('user.edit',$user->id).'" title="Edit User" class="btn btn-sm" style="color: #fff;background-color: #3DCB3A;border-color: #8ad3d3"> <i class="fa fa-edit"></i> </a> 
                        <a href="'.route('user.destroy',$user->id).'" class="btn btn-danger btn-sm" title="Hapus User" ><i class="fa fa-trash "></i></a>';
                        return $actionBtn;
                    })
                    ->rawColumns(['action'])
                    ->make(true);
            }
        return view('dashboard.user.user');
        // return response()->json($list_user);
    }
}

CodePudding user response:

i fix button delete with alert

this the Http/Controllers/UserController.php

    public function index(Request $request)
    {
        // $list_user = User::all();
        if($request->ajax()){
            $user = User::latest()->get();
            return datatables()->of($user)
                    ->addIndexColumn()
                    ->addColumn('action', function (User $user){
                        $actionBtn = '<a href="'.route('user.edit',$user->id).'" title="Edit User" class="btn btn-sm" style="color: #fff;background-color: #3DCB3A;border-color: #8ad3d3"> <i class="fa fa-edit"></i> </a> 
                        <a href="javascript:void(0)" class="btn btn-danger btn-sm" title="Hapus User" onclick="hapus('.$user->id.')" ><i class="fa fa-trash "></i></a>';
                        return $actionBtn;
                    })
                    ->rawColumns(['action'])
                    ->make(true);
            }
        return view('dashboard.user.user');
        // return response()->json($list_user);
    }

this is the resources/views/dashboard/user/user.blade.php add this in top @section

@push('stylesheets')
  <link rel="stylesheet" href="{{asset('assets/plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css')}}">
@endpush

and add this in bottom section

@push('javascripts')
<script src="{{asset('assets/plugins/sweetalert2/sweetalert2.min.js')}}"></script>
<script type="text/javascript">
   $(function () {
    
    var table = $('.yajra-datatable').DataTable({
          processing  : true,
          serverSide : true,
          ajax :"{{route('user.index')}}",  
          columns: [
                {data: 'DT_RowIndex', name: 'DT_RowIndex'},
                {data: 'name', name:'name'},
                {data: 'username', name:'username'},
                {data: 'email', name:'email'},
                {data: 'action', name: 'action', orderable: false, searchable: false},
                  ],
        });
    } );
    function hapus(e) {
    var url = '{{ route("user.destroy", ":id") }}';
        url = url.replace(':id', e);
    $.ajaxSetup({
    headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
    });
      Swal.fire({
          title             : "Apakah Anda Yakin ?",
          text              : "Data Yang Sudah Dihapus Tidak Bisa Dikembalikan!",
          icon              : "warning",
          showCancelButton  : true,
          confirmButtonColor: "#3085d6",
          cancelButtonColor : "#d33",
          confirmButtonText : "Ya, Tetap Hapus!"
      }).then((result) => {
          if (result.value) {
              $.ajax({
                  url    : url,
                  type   : "delete",
                  success: function(data) {
                    $('.yajra-datatable').DataTable().ajax.reload();
                  }
              })
          }
      })
  }
</script>
@endpush
  • Related