CakePHP 4 Remove Column From Table Using Migration

Share this Article
Reading Time: 3 minutes
1,495 Views

Inside this article we will see CakePHP 4 remove column from table using migration. Article contains classified information about updating table schema using cakephp migration concept.

Migrations are those files which creates table schema for application.

Let’s assume we have a table schema for products. Product table contains columns and their attributes as –

  • id [INTEGER]
  • name [STRING]
  • cost [INTEGER]
  • product_image [STRING]
  • description [TEXT]
  • status [ENUM]
  • created_at [TIMESTAMP]

We want to remove column product_image from this table. We will alter table for this case. We will delete column from above table.

CakePHP CLI gives a smart option to create migration files for schema. We will use bin/cake CLI to generate migration files.

Learn More –

Let’s get started.


CakePHP 4 Installation

To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.

$ composer create-project --prefer-dist cakephp/app:~4.0 mycakephp

Above command will creates a project with the name called mycakephp.


Database Connection

Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.

You can add your connection details here to connect with your database. It will be like this –

//...

'Datasources' => [
        'default' => [
            'host' => 'localhost',
            /*
             * CakePHP will use the default DB port based on the driver selected
             * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
             * the following line and set the port accordingly
             */
            //'port' => 'non_standard_port_number',

            'username' => 'root',
            'password' => 'sample@123',

            'database' => 'mydatabase',
            /*
             * If not using the default 'public' schema with the PostgreSQL driver
             * set it here.
             */
            //'schema' => 'myapp',

            /*
             * You can use a DSN string to set the entire configuration
             */
            'url' => env('DATABASE_URL', null),
        ],
  
     //...

//...

You can pass host, username, password and database.

Successfully, you are now connected with the database.


Create & Run Migration – Alter Table

Open project into terminal and run this command

$ bin/cake bake migration RemoveImageColumn

This command will create a migration file inside /config/Migrations folder. File name will be prefixed with the timestamp value and look like 20220217030354_RemoveImageColumn.php

Open migration file and write this code into it to alter “products” table schema.

<?php

declare(strict_types=1);

use Migrations\AbstractMigration;

class RemoveImageColumn extends AbstractMigration
{
    /**
     * Change Method.
     *
     * More information on this method is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     * @return void
     */
    public function change()
    {
        $table = $this->table('products');

        $table->removeColumn('product_image');
        
        $table->save();
    }
}

Run Migrations

Back to terminal and run this command to migrate this migration. It will update table schema of database

$ bin/cake migrations migrate

When it executes we should see updated “products” table in database. From table “products” column product_image should be removed.

For more about datatypes and other information of migration you can click here for official document.

We hope this article helped you to learn about CakePHP 4 Remove Column From Table Using Migration Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5