Ad

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); 
$record->delete();

}

In twig i have

 <tbody>
     {% 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>

    </tr>

    {% endfor %}

 </tbody>

I am currently having this error

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

Ad

Answer

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

Add data-request-data="record: {{ post.id }}" 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.

Ad
source: stackoverflow.com
Ad