Database
Migrations
Micro migrations: Are the Laravel built-in migrations so please refer to the official docs and the
artisan migrate:*commands.Service migrations: When a service is generated with the
lucid make:servicecommand, it will contain adatabasedirectory that resemble/databaseat root.
app/Services/Chat/database
├── factories
├── migrations
└── seeders
The examples in this document assume that our service name is Chat, please make sure to replace it with your service name instead.
Generate Service Migration
Signature lucid make:migration <migration> <service>
Example
lucid make:migration create_messages_table chat
Will generate app/Services/Chat/database/migrations/{date}_create_messages_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('messages');
}
}
Run Service Migrations
Our migrations are recognized by Laravel due to them being loaded automatically in the service provider - ChatServiceProvider in this example.
Which makes it straight-forward to run them:
php artisan migrate
Disable Service Migrations
If your service doesn’t use a [separate] database (you may always still use Laravel’s default migrations) and you wish to clean it up from database stuff:
- Remove
app/Services/Chat/databasedirectory - Remove the following snippet from
ChatServiceProvider::bootinapp/Services/Chat/Providers/ChatServiceProvider.php$this->loadMigrationsFrom([ realpath(__DIR__ . '/../database/migrations') ]);
Factories
Register service factories in composer
- Under
autoload.psr-4add
"App\\Services\\Chat\\Database\\Factories\\": "app/Services/Chat/database/factories/"So your
composer.jsonshould have a similar section as this:{ "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/", "App\\Services\\Chat\\Database\\Factories\\": "app/Services/Chat/database/factories/" } } }- Run
composer dump-autoload
- Under
Create factory class (e.g.
php artisan make:factory --model=Message)Move factory class from
/database/factoriesto/app/Services/Chat/database/factoriesChange class namespace to
App\Services\Chat\Database\FactoriesIn your model class (here it’s
Message): addnewFactory()method. This is a method override that Laravel natively looks at before loading the default factory (seeHasFactorytrait)use App\Services\Chat\Database\Factories\MessageFactory; protected static function newFactory() { return app(MessageFactory::class); }Use the factory from the model as you would by default:
Message::factory()->make()
Seeders
Register service seeders in composer
- Under
autoload.psr-4add the following
"App\\Services\\Chat\\Database\\Seeders\\": "app/Services/Chat/database/seeders/"So your
composer.jsonshould have a similar section as this:{ "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/", "App\\Services\\Chat\\Database\\Seeders\\": "app/Services/Chat/database/seeders/" } }, }- Run
composer dump-autoload
- Under
Create seeder class (e.g.
php artisan make:seeder MessageSeeder)Move seeder class from
/database/seedersto/app/Services/Chat/database/seedersChange class namespace to
App\Services\Chat\Database\SeedersCall
MessageSeeder::classfrom/database/seeders/DatabaseSeeder.phpuse App\Services\Chat\Database\Seeders\MessageSeeder; public function run() { $this->call([ MessageSeeder::class, ]); }Seed the database with
php artisan db:seed