How to create and drop tables in WHMCS 7 using PHP 7?

20 Sep

How to create and drop tables in WHMCS 7 using PHP 7?

We might have to create a table from WHMCS addon while activating the module.
Also we might have to drop the table while deactivating the module.
Here we are going to write PHP 7 compatible codes to create and drop the table.

Create a table

Assume that we have to create a table named sample_table
with the following fields.
It’s an auto increment field, also it’s primary key of the table.

This field is an integer

This field should store string and maximum character length is 100.

It’s a date field

It’s float field with two decimal points.

It’s a text field.

Then we can use below code for that.

 use Illuminate\Database\Capsule\Manager as Capsule;
 if (!Capsule::schema()->hasTable('sample_table')) {
        Capsule::schema()->create('sample_table', function($table) {
            $table->string('name', 100);
            $table->float('amount', 8, 2);

Drop the table.

To drop the table we can use the below code.

#add the below line only once in a file
use Illuminate\Database\Capsule\Manager as Capsule;

Leave a Reply

Your email address will not be published. Required fields are marked *