Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Bulk Delete

Asyraf Wahi Anuar Updated 2 min read

This tutorial will show how to create a bulk delete function in CakePHP 4. The bulk delete is a process of selecting a group of records in a single table and removing it from the database. A checkbox will be rendered on each of the record IDs and sent to the function that manages the delete all proc...

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

This tutorial will show how to create a bulk delete function in CakePHP 4. The bulk delete is a process of selecting a group of records in a single table and removing it from the database. A checkbox will be rendered on each of the record IDs and sent to the function that manages the delete all process.

Create Checkbox

A checkbox is used to capture the id of each selected record that need to be deleted. The checkbox is generated in the index page as shown below:

templates/Users/index.phpPHP
<table id="main_index" class="table table-hover text-nowrap">
<thead>
	<tr>
		<th><?= $this->Paginator->sort('#') ?></th>

		...other codes

	<?= $this->Form->create(null,['url'=>['action'=>'bulkDelete']]) ?>
	<?php foreach ($users as $user): ?>
	<tr>
		<td><?= $this->Form->checkbox('ids[]',['value'=>$user->id]) ?></td>
</table>
	<button class="button">Delete Selected</button>
	<?= $this->Form->end() ?>
	<?= $this->fetch('postLink'); ?>

Public Function bulk delete

Create the public function that handles the bulk delete process as shown below in your controller.

src/Controller/UsersController.phpPHP
public function bulkDelete()
{
	$this->request->allowMethod(['post','delete']);
	$ids = $this->request->getData('ids');
	
	if($this->Users->bulkDelete(['Users.id IN'=>$ids])){
		$this->Flash->success(__('The selected user has been deleted.'));
	} else {
		$this->Flash->error(__('Please select user to delete'));
	}
		return $this->redirect(['action' => 'index']);
		//exit('test');
}

Now you should be able to select and delete the records.

If you want to have a main checkbox in your header that will select all or deselect all the checkboxes with one single click, use this code in your header:

PHP
<th><?= $this->Form->checkbox('ids[]',['onchange'=>'checkAll(this)', 'name'=>'chk[]']) ?></th>

Then add the following script at the end of your code:

PHP
<script>
 function checkAll(ele) {
     var checkboxes = document.getElementsByTagName('input');
     if (ele.checked) {
         for (var i = 0; i < checkboxes.length; i++) {
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = true;
             }
         }
     } else {
         for (var i = 0; i < checkboxes.length; i++) {
             console.log(i)
             if (checkboxes[i].type == 'checkbox') {
                 checkboxes[i].checked = false;
             }
         }
     }
 }
</script>

That all. Happy coding :)

Did this tutorial help?

237 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read