The final pillar of the CRUD interface is the ability to delete items. In concrete5, this is a little bit more simple than the other CRUD tasks, as deleting items typically doesn't require a visual component. In this recipe, we will delete a blog post and simply redirect the user back to the index view of the posts.
We will continue working with the concept of simple blog posts. This recipe assumes that you have completed the previous recipes in this chapter and have the appropriate database tables created.
Open the controller file located at
/controllers/dashboard/posts.php
.Declare a new function called
delete
that has one parameter,$id
.public function delete($id) { }
In the
delete
function, load theblog posts
model.Loader::model('blog_post');
Create a new instance of the model.
$post = new BlogPost();
Load the post by its ID.
$post->load('id = ?', $id);
Delete the post by calling the
delete
function.$post->delete();
Redirect the user...