Database

Laracord has full support for Laravel's ORM Eloquent and utilizes it out of the box for the User model alongside SQLite.

Note

For extensive documentation on Eloquent, we highly suggest taking a look at the Laravel documentation.

Using MySQL

To use MySQL instead of Laracord's default SQLite driver, you may set the DB_CONNECTION to mysql and provide the database credentials in your .env file:

DB_CONNECTION=mysql
DB_HOST=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

Creating a Model

Creating a database model can be done using the laracord binary:

$ php laracord make:model Balance

If you would like to generate a database migration when creating the model, you may use the -m option:

$ php laracord make:model Balance -m

Models generated by the make:model command will be placed in the app/Models directory. The following is a basic example of a generated model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Balance extends Model
{
    // ...
}

Creating a Migration

Outside of using the -m option on make:model, you may also generate a migration using the make:migration console command:

$ php laracord make:migration create_balances_table

Generated migrations will be placed in the database/migrations directory. Each migration filename contains a timestamp which allows Laravel to know the order of the migrations.

The following is a simple migration that would reflect a Balances table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('balances', function (Blueprint $table) {
            $table->id();
            $table->string('discord_id');
            $table->string('server_id');
            $table->integer('amount')->default(0);
            $table->unique(['discord_id', 'server_id']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('balances');
    }
};

In the migration above, the User's discord_id and server_id are both stored and made unique when used in combination with one another. Alongside that is the amount consisting of the user's balance.