2. Database Management - nathan-fiscaletti/synful GitHub Wiki
Note: If you want a more in depth explanation of everything in Synful's configuration, please see Config Run-through.
Accessing the Database Configuration
To start, let's open up the database configuration file located in config/Databases.json.
Synful uses the Eloquent ORM for it's database layer. You can see more detailed documentation on this here
Configuring your databases
An example database configuration
"synful": {
"driver": "mysql",
"host": "127.0.0.1",
"database": "synful",
"username": "root",
"password": "password",
"charset": "utf8",
"collation": "utf8_unicode_ci",
"prefix": ""
}
Note: The default database that Synful will use is defined under the key
synful
. You can create other database connections and put them under other keys. When you create a new Model or Migration, you can override the$connection
variable to select a different database connection.
Migrations
Creating Migrations
To create a new migration, use the following command:
$ ./synful -create-migration MyMigration my_table
This will create a new Migration using the defined migration name and table name in ./src/Synful/App/Data/Migrations/
. Each migration has an up
and a down
function. Override these to control what happens when the migration is run.
Running Migrations
To run your migrations, use the following command:
$ ./synful -migrate [up|down]
Note: When you run the
-migrate
command, all migrations will be run. You cannot select which migration you want to run specifically.
Example Migration
Note: Use
$this->schema()
to access the Schema. See Migrations for documentation onillumnate/database
Migrations.
<?php
namespace Synful\App\Data\Migrations;
use Illuminate\Database\Schema\Blueprint;
use Synful\Util\Data\Migrations\Util\Migration;
class CreateIpFirewallTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
$this->schema()->create(
'ip_firewall', function (Blueprint $table) {
$table->increments('id');
$table->string('ip');
$table->boolean('block');
$table->integer('api_key_id');
$table->timestamps();
}
);
}
/**
* Reverse the migrations.
*/
public function down()
{
$this->schema()->drop('ip_firewall');
}
}
Models
To create a new model use the following command:
$ ./synful -create-model ModelName table_name
This will create a new model in ./src/Synful/App/Data/Models/
.
See Eloquent for documentation on Eloquent Models.
Executing SQL
To execute SQL directly, you can use the static function capsule()
in the Database.php class.
Example
use \Synful\Util\Data\Database as DB;
. . .
$result = DB::capsule()->getConnection('myDatabase')->select(
'select * from table'
);
Alternately, you can call the connection name directly as a static function on the Database.php class.
Example
use \Synful\Util\Data\Database as DB;
. . .
$result = DB::myDatabase()->select(
'select * from table'
);
Next: Request Handlers