0

when i click delete all selected its saying undefined variable however when i check the database the post gets deleted and when i refresh the page the post gets removed from the page. I don't understand why its bringing an error when its working.enter image description here

blade

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script>
<meta name="csrf-token" content="{{ csrf_token() }}">
Delete All Selected post Name post Details Action @foreach($posts as $key => $post) id}}"> id}}"> {{ ++$key }} {{ $post->about }} {{ $post->image }} id}}" class="btn btn-danger btn-sm" data-tr="tr_{{$post->id}}" data-toggle="confirmation" data-btn-ok-label="Delete" data-btn-ok-icon="fa fa-remove" data-btn-ok-class="btn btn-sm btn-danger" data-btn-cancel-label="Cancel" data-btn-cancel-icon="fa fa-chevron-circle-left" data-btn-cancel-class="btn btn-sm btn-default" data-title="Are you sure you want to delete ?" data-placement="left" data-singleton="true"> Delete @endforeach
<script type="text/javascript">
    $(document).ready(function () {


        $('#master').on('click', function(e) {
         if($(this).is(':checked',true))
         {
            $(".sub_chk").prop('checked', true);
         } else {
            $(".sub_chk").prop('checked',false);
         }
        });


        $('.delete_all').on('click', function(e) {


            var allVals = [];
            $(".sub_chk:checked").each(function() {
                allVals.push($(this).attr('data-id'));
            });


            if(allVals.length <=0)
            {
                alert("Please select row.");
            }  else {


                var check = confirm("Are you sure you want to delete this row?");
                if(check == true){


                    var join_selected_values = allVals.join(",");


                    $.ajax({
                        url: $(this).data('url'),
                        type: 'DELETE',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'ids='+join_selected_values,
                        success: function (data) {
                            if (data['success']) {
                                $(".sub_chk:checked").each(function() {
                                    $(this).parents("tr").remove();
                                });
                                alert(data['success']);
                            } else if (data['error']) {
                                alert(data['error']);
                            } else {
                                alert('Whoops Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            alert(data.responseText);
                        }
                    });


                  $.each(allVals, function( index, value ) {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }
            }
        });


        $('[data-toggle=confirmation]').confirmation({
            rootSelector: '[data-toggle=confirmation]',
            onConfirm: function (event, element) {
                element.trigger('confirm');
            }
        });


        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();


            $.ajax({
                url: ele.href,
                type: 'DELETE',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) {
                        $("#" + data['tr']).slideUp("slow");
                        alert(data['success']);
                    } else if (data['error']) {
                        alert(data['error']);
                    } else {
                        alert('Whoops Something went wrong!!');
                    }
                },
                error: function (data) {
                    alert(data.responseText);
                }
            });


            return false;
        });
    });
</script>


</html>
```

controller

public function showem(Post $post)
{
  $posts = Post::get();
  


  return view('users.registered', compact('posts'));
}
public function deleteAll(Request $request)
{
    $ids = $request->ids;
    $deleted = Post::whereIn('id',explode(",",$ids))->delete();
    return view('users.registered');
}
6
  • Which variable is undefined here? Commented Jul 14, 2020 at 16:54
  • sorry was still editing the question. $post variable Commented Jul 14, 2020 at 16:55
  • Please show the controller code as well. Commented Jul 14, 2020 at 16:56
  • @aynber controller added Commented Jul 14, 2020 at 17:16
  • @STA the image of the error has been added in the post Commented Jul 14, 2020 at 17:32

1 Answer 1

0

After review your full question again and again, I found that you have got this error on detele method.
You need to send posts variable also on delete method :

public function showem(Post $post)
{
    $posts = Post::get();
    return view('users.registered', compact('posts'));
}

public function deleteAll(Request $request)
{
    $ids = $request->ids;
    $deleted = Post::whereIn('id',explode(",",$ids))->delete();
    $posts = Post::get();
    return view('users.registered', compact('posts')); // here send the posts variable
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.