Declare database schemas & factory definitions inside Laravel models.
Require Lucid via composer:
composer require kjjd84/lucidReplace the default User model in new apps:
php artisan lucid:model User --forceCreate a new Lucid model:
php artisan lucid:model PostYou may also add schema and definition methods to existing models:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kjjd84\Lucid\Database\Blueprint;
class Post extends Model
{
use HasFactory;
public function schema(Blueprint $table): void
{
$table->id();
$table->string('title');
$table->text('body');
$table->timestamp('created_at');
$table->timestamp('updated_at');
}
public function definition(): array
{
return [
'title' => fake()->sentence(),
'body' => fake()->paragraph(),
];
}
}If adding methods manually, make sure to create a Lucid factory for the model:
php artisan lucid:factory PostDefine multiple schemas & definitions by appending Schema / Definition:
namespace App\Traits;
use App\Models\Tenant;
use Kjjd84\Lucid\Database\Blueprint;
trait HasTenant
{
public function tenantSchema(Blueprint $table): void
{
$table->integer('tenant_id')->index();
}
public function tenantDefinition(): array
{
return [
'tenant_id' => Tenant::factory(),
];
}
}Migrate & sync schemas with the database:
php artisan lucid:migrateThis runs traditional migrations first, then syncs schema methods automatically.
Create a new Lucid model.
php artisan lucid:model {name} {--force} {--p|pivot} {--r|resource}name: the model name--force: Create the model even if it already exists--pivotor-p: Create a pivot instead of a regular model--resourceor-r: Create a Filament resource for the model
Create a new Lucid factory.
php artisan lucid:factory {name} {--force}name: the model name for the factory--force: Create the factory even if it already exists
Migrate & sync schemas with the database.
php artisan lucid:migrate {--force} {--f|fresh} {--s|seed}--force: Force the operation to run when in production--freshor-f: Drop all tables from the database first--seedor-s: Re-run seeders when migrations are complete
- This package only works with
sqlite,mysql, &pgsqlPDO drivers - Renaming columns will result data loss unless renamed before running
lucid:migrate - Lucid definition methods only work with a
LucidFactory - All columns (except
id) are nullable by default - All models are unguarded via
Model::unguard()