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:service
command, it will contain adatabase
directory that resemble/database
at 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/database
directory - Remove the following snippet from
ChatServiceProvider::boot
inapp/Services/Chat/Providers/ChatServiceProvider.php
$this->loadMigrationsFrom([ realpath(__DIR__ . '/../database/migrations') ]);
Factories
Register service factories in composer
- Under
autoload.psr-4
add
"App\\Services\\Chat\\Database\\Factories\\": "app/Services/Chat/database/factories/"
So your
composer.json
should 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/factories
to/app/Services/Chat/database/factories
Change class namespace to
App\Services\Chat\Database\Factories
In your model class (here it’s
Message
): addnewFactory()
method. This is a method override that Laravel natively looks at before loading the default factory (seeHasFactory
trait)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-4
add the following
"App\\Services\\Chat\\Database\\Seeders\\": "app/Services/Chat/database/seeders/"
So your
composer.json
should 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/seeders
to/app/Services/Chat/database/seeders
Change class namespace to
App\Services\Chat\Database\Seeders
Call
MessageSeeder::class
from/database/seeders/DatabaseSeeder.php
use App\Services\Chat\Database\Seeders\MessageSeeder; public function run() { $this->call([ MessageSeeder::class, ]); }
Seed the database with
php artisan db:seed