How To Delete A Record In Octobercms?

- 1 answer

I am a bit confused how it comes together. I am trying to delete a record from a list of records and I currently have this.

function onDelete()
$recordId = post('record')
$record = Advert::find($recordId); 


In twig i have

     {% for post in posts %}

      <td><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="{{ 'product-single'|page({ slug: post.slug }) }}" name = "record">{{ post.title }}</a></td>

      <td><button class="btn btn-sm btn-danger" data-request ="onDelete" data-request-confirm="Are you sure" data-request-success="alert('Successfully Deleted')">Delete</button> </td>


    {% endfor %}


I am currently having this error

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



You're not sending the record ID to the server in your AJAX request.

Add data-request-data="record: {{ }}" to your button that triggers the AJAX request so that post('record') actually gets data from the AJAX request that it can use to find the record that you want to delete.