Skip to content

Database

Zane Hooper edited this page Mar 16, 2017 · 12 revisions

Migrations

All packages have full access to modifying the database schema via Laravel migrations. It is recommended, though not required, to prefix all table names with pkg_<your package name>_, e.g. pkg_abuse_.

Creating a Migration

To create a migration, create a class in the package's database/migrations directory and make it extend App\Support\Database\Migration. The Migration class provides handy methods for interacting with the SynergyCP application.

Once you have completed writing the migration, you can run this command from the main application:

php artisan migrate --path=packages/$PACKAGE/database/migrations

and php artisan migrate:rollback to roll back the migration.

Custom Types

There are some custom types made available by the Migration class. Make sure to use $this->schema()-> instead of Schema:: to get access to the custom Blueprint. The custom Blueprint class (usually stored as $table) has the following methods for custom types:

    /**
     * A column that stores the binary representation of an IP address.
     *
     * @param string $column
     *
     * @return \Illuminate\Support\Fluent
     */
    public function ip($column);

    /**
     * @param string $column
     * @param array $parameters
     *
     * @return \Illuminate\Support\Fluent
     */
    public function varbinary($column, array $parameters);

Example Usage:

    $this->schema()->create('some_table', function (Blueprint $table) {
        $table->increments('id');
        $table->ip('ip')->unique();
        $table->timestamps();
    });

Settings

The App\Support\Database\Migration class has helpful methods for adding Settings and Setting Groups:

    /**
     * @param string $name
     * @param array  $info
     *
     * @return SettingGroup
     */
    protected function addSettingGroup($name, array $info = []);

    /**
     * @param SettingGroup $group
     * @param string       $type
     * @param string       $name
     * @param array        $info
     *
     * @return Setting
     */
    protected function addSetting(SettingGroup $group, $type, $name, array $info = []);

Example Usage:

public function up()
{
    $group = $this->addSettingGroup('Abuse Reports');
    $this->addSetting($group, Setting::TYPE_TEXT, 'pkg.abuse.report.threshold', [
        'validator' => Setting::VALID_INT,
        'value' => 7,
    ]);
}

public function down()
{
    $this->deleteSettingGroup('Abuse Reports');
}

Once a Setting has been added to the database (after the migrations have been run) you will see it on the Settings page of the application. The setting value is accessible at app()->Settings->{$settingName}.

Email Templates

    /**
     * @param string $name
     * @param string $file
     * @param array  $data
     *
     * @return EmailTemplate
     */
    protected function addEmailTemplate($name, $file, array $data = []);

    /**
     * @param string $name
     */
    protected function removeEmailTemplate($name);