# CakePHP 4 CRUD

> CRUD operation in CakePHP enables the developer to rapidly generate the prototype for a web application. CRUD comprise routine code for create, read, update and delete operation.

- URL: http://codethepixel.com/cakephp-4-crud
- Author: Asyraf Wahi Anuar (http://codethepixel.com/author/asyraf-wahi-anuar)
- Published: 2020-05-10
- Updated: 2021-11-02
- Category: CakePHP
- Tested with: CakePHP 4.x
- Tags: CakePHP, CRUD
- Reading time: 2 min

CRUD creation using console commands is one of my favourite features in CakePHP. This tutorial will guide you on how to generate CRUD based on the database using console commands. Console applications are ideal for handling a variety of background & maintenance tasks that leverage your existing application configuration, models, plugins and domain logic.

## Database configuration

The database should be connected with the CakePHP framework. Refer here to configure the database connection.

Execute the Command Prompt/ Windows Power Shell/ Git Bash and navigate the target directory to your development folder eg: myCake4 folder, using the change directory command:

```bash
$ cd c:/xampp/htdocs/myCake4
```

Execute this command to generate the CRUD based on the configured database table (eg: users table):

```bash
$ bin\cake bake all users
```

> **Screenshot unavailable:** The original screenshot (crud-min.jpg) was lost when the old site moved. The text describes what it showed.

It will generate:

- Model (...src/Model/Entity/User.php & ...src/Model/Entity/UsersTable.php)
- Controller (.../src/Controller/UsersController.php)
- View (...templates/Users/\[add.php, edit.php, view.php & index.php\])
- Tests file

Now, navigate to localhost/myCake4/users and it should render the index page for the user table as follows:

> **Screenshot unavailable:** The original screenshot (index-min.jpg) was lost when the old site moved. The text describes what it showed.

The add new user page will render as follows:

> **Screenshot unavailable:** The original screenshot (add-min.jpg) was lost when the old site moved. The text describes what it showed.

Repeat for other tables in your database.

That all. Happy coding :)
