Osuji Kingsley
Osuji Kingsley

Reputation: 371

How to delete a record in octobercms?

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 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"

Upvotes: 0

Views: 471

Answers (2)

Chathura Madhushanka
Chathura Madhushanka

Reputation: 71

Make sure to use field names when querying. Try this.

function onDelete()
{
$recordId = request('post')
Advert::where('slug',$recordId)->delete(); 
}

If not add die() and use print_r to check whether Advert::where is returning any object.

Upvotes: 0

LukeTowers
LukeTowers

Reputation: 1291

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.

Upvotes: 2

Related Questions