简体   繁体   English

如何删除octobercms中的记录?

[英]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" “调用成员函数delete()时为null”

You're not sending the record ID to the server in your AJAX request. 您没有在AJAX请求中将记录ID发送到服务器。

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. data-request-data="record: {{ post.id }}"到触发AJAX请求的button ,以便post('record')实际上从AJAX请求中获取数据,该数据可用于查找该记录。您要删除。

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. 如果没有,请添加die()并使用print_r检查Advert :: where是否返回任何对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM