Adding a column to a table in MySQL database

Hi there,

I would like to add a column to a table in a connected MySQL database through Kirby.

This is where I’m at:

I connected to the database in config.php:

'db' => [
        'host'     => 'xxx',
        'database' => 'xxx',
        'user'     => 'xxx',
        'password' => 'xxx',

In the snippet, through which the column is supposed to be added:
I select an instance of the database.
I validate if the table exists.
I validate if the column already exists in the table.

$database = Database::instance();

$tableName = 'my_table';

if( $database->validateTable($tableName) ){
  $columnName = 'new_column';
  if( $database->validateColumn($tableName, $columnName) ){
    // column exists
  } else {
    // create column
    $props = array( 'type' => 'text' );
    // what's next?


I would need something like:


Or some sql query maybe :thinking:

There is a createColumn() method in the Sql class. It should be accessible via $database->sql()

Thank you!

I tried to apply it like below, but it has no effect:

// create column
$props = array( 'type' => 'text' );
$sql = $database->sql();

How does it know which table to add the column to? :thinking:

:partying_face: Mostly by luck I was able to resolve it this way:

// create column
$database->execute('ALTER TABLE ' . $tableName . ' ADD ' . $columnName . ' text');