Installation:
composer require saher/artisan-schematics --dev
Add the service provider to config/app.php under providers:
Saher\ArtisanSchematics\ArtisanSchematicsServiceProvider::class,
Publish Config:
php artisan vendor:publish --provider="Saher\ArtisanSchematics\ArtisanSchematicsServiceProvider" --tag="config"
This generates config/artisan-schematics.php. Configure default output paths and enabled languages (e.g., typescript, dart).
First Use Case: Generate TypeScript definitions for all models:
php artisan schematics:generate
Outputs to resources/lang/typescript (or your configured path) by default.
Generate Schematics:
php artisan schematics:generate
app/Models (configurable via model_paths in config).Selective Generation: Generate for specific models:
php artisan schematics:generate --models="User,Post"
Generate for a single language:
php artisan schematics:generate --languages="dart"
Watch Mode (for development):
php artisan schematics:watch
Customize Output Paths:
Override paths per language in config/artisan-schematics.php:
'paths' => [
'typescript' => 'resources/lang/typescript',
'dart' => 'lib/generated',
],
Extend Generators: Create a custom generator (e.g., for Rust):
php artisan vendor:publish --provider="Saher\ArtisanSchematics\ArtisanSchematicsServiceProvider" --tag="generators"
app/Generators/RustGenerator.php extending Saher\ArtisanSchematics\Generators\Generator.config/artisan-schematics.php:
'generators' => [
\App\Generators\RustGenerator::class,
],
Hook into Model Events:
Regenerate schematics after model changes via ModelObserver:
use Saher\ArtisanSchematics\Facades\Schematics;
class ModelObserver {
public function saved($model) {
if ($model instanceof \Illuminate\Database\Eloquent\Model) {
Schematics::generate();
}
}
}
CI/CD Pipeline: Add to deployment script to ensure frontend/backend type safety:
php artisan schematics:generate --languages="typescript,dart" --force
Circular Dependencies:
User hasMany Post, Post belongsTo User), the package handles it but may generate verbose output. Use --minimal flag to reduce redundancy:
php artisan schematics:generate --minimal
Custom Casts Not Detected:
protected $casts = [
'metadata' => 'array', // Works
'status' => CustomCast::class, // Requires CustomCast to implement `getValueForSerialization`
];
Language-Specific Quirks:
interface for models and enum for PHP enums. Nested relationships are flattened into union types.class for models and class for enums (with @immutable if enabled).File Overwrites:
--force to overwrite:
php artisan schematics:generate --force
Verbose Output:
Enable debug mode in config/artisan-schematics.php:
'debug' => true,
Or use the --verbose flag:
php artisan schematics:generate --verbose
Dry Run: Preview changes without writing files:
php artisan schematics:generate --dry-run
Check Generated Files:
Custom Field Mappings:
Override field naming or types per model via a getSchematicsAttributes method:
class User extends Model {
public function getSchematicsAttributes(): array {
return [
'name' => ['type' => 'string', 'alias' => 'fullName'],
'email' => ['type' => 'string', 'format' => 'email'],
];
}
}
Pre/Post-Generation Hooks:
Bind events in EventServiceProvider:
protected $listen = [
'Saher\ArtisanSchematics\Events\SchematicsGenerating' => [
\App\Listeners\LogGenerationStart::class,
],
'Saher\ArtisanSchematics\Events\SchematicsGenerated' => [
\App\Listeners\NotifySlack::class,
],
];
Template Overrides:
Customize the output template for a language by extending the generator’s getTemplate method or overriding the Blade template in resources/views/vendor/artisan-schematics.
Enum Handling:
Exclude PHP enums from generation by adding to config/artisan-schematics.php:
'exclude_enums' => [
\App\Enums\InternalStatus::class,
],
How can I help you explore Laravel packages today?