user16218026
user16218026

Reputation:

Laravel/Ajax DELETE: "Call to a member function delete() on null"

As the title says, I am getting an error when I try to delete my data using laravel/ajax and the function inside the controller:

1

Controller:

public function destroy($category_id) {
    $category_delete = HmsBbrCategory::find($category_id);
    $category_delete->delete();
    return response()->json([
        'status'=>200,
        'message'=>'Category Deleted!',
    ]);

}

Form & Ajax:

`delete_category` is the id that opens the delete modal and `delete_category_btn` is the button that will delete the data.

<button type="submit" class="btn btn-primary btn-block delete_category_btn"></i>Yes</button>
<button type="button" value="${cat.category_id}" class="delete_category btn btn-outline-secondary"><i class="fas fa-trash"></i> Delete</button>


        $(document).on('click', '.delete_category', function (e) {
            e.preventDefault();
            //click this button(delete_category) to get the value(category_id)
            var cat_id = $(this).val(); 
            // alert(cat_id);
            $('#delete_cat_id').val(cat_id);
            $('#deleteCategoryModal').modal('show');
        });
        $(document).on('click', '.delete_category_btn', function (e) {
            e.preventDefault();
            var cat_id = $('#delete_cat_id').val();
            // alert(cat_id);
            //token taken from laravel documentation
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: "DELETE",
                url: "/clinical/bbr-category-configuration-delete/"+cat_id,
                dataType: "dataType",
                success: function (response){
                    // console.log(response);
                    $('#category_notif').addClass('alert alert-success');
                    $('#category_notif').text(response.message);
                    $('#deleteCategoryModal').modal('hide');
                }
            });
        });

Route:

Route::delete('/bbr-category-configuration-delete/{category_id}', [BBRCategoryConfigurationController::class,'destroy']);

Things to note:

the the second ajax function for the delete button: $(document).on('click', '.delete_category_btn', function (e), I also tried to show the id using alert(cat_id); to also prove that the id is still getting recognized inside the modal:

2

Even if category_id is present, inspecting the page after attempting to delete still shows:

message: "Call to a member function delete() on null"

What can I do to resolve this? thanks for any help.

Upvotes: 0

Views: 480

Answers (1)

Mohammad Hosseini
Mohammad Hosseini

Reputation: 1807

$category_delete object is null. Maybe you are sending a wrong $category_id. If you check that the category exists before delete you avoid that error.

public function destroy($category_id) {
    $category_delete = HmsBbrCategory::find($category_id);

    if($category_delete) {
        $category_delete->delete();
        return response()->json([
            'status'=>200,
            'message'=>'Category Deleted!',
        ]);
    }
    
    return response()->json([
        'status'=>404,
        'message'=>'Category Not Found!',
    ]);
}

Upvotes: 2

Related Questions