Repository: laravel-shift/blueprint (language: php)

Issue:
Ability to add connection/table name in model 
- Laravel Version: 10.4.0
- PHP Version: 8.2.0
- Blueprint Version: 2.7.0
- Platform: Windows

### Issue:
Ability to add connection/table name in model 

### draft.yaml:
```yaml
Models:
    SystemInformationConversion:
         range: string:100
         rubric: string:200
         specified_value: string:50
         softDeletes: true
         timestamps: false

         table: system_information_conversion
         connection: db_connection_name
```



Discussion / hints:
You can do this with the `meta` tag, right? I assume that's why you closed it?
Thank you for your reply. I have closed it because it's not actually a bug.

i think the solution is in class ModelLexer implements Lexer
private function buildModel(string $name, array $columns) {

// for table we need this condition
if (isset($columns['table'])) {
if ($columns['table']) {
$model->setTableName($columns['table']);
unset($columns['table']);
}
} do you have other solution? my draft.yaml is
models:
SystemAngabenKonvertierung:
bereich: string:100
rubrik: string:200
angabe_wert: string:50
angabe_zusatz: string:100
info_text: text
konvert_alt_system: string:100
softDeletes: true
timestamps: false
table: sta_system_angaben_konvertierung

On Wed, May 17, 2023, 15:29 Jason McCreary ***@***.***> wrote:

> You can do this with the meta tag, right? I assume that's why you closed
> it?
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/laravel-shift/blueprint/issues/639#issuecomment-1551403524>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AC47PPZ6HBQ5TU6KA3QFSRLXGTHFLANCNFSM6AAAAAAYEZP3UY>
> .
> You are receiving this because you modified the open/close state.Message
> ID: ***@***.***>
>


Fix patch:
diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php
index d4075db8..895fdf12 100644
--- a/src/Generators/MigrationGenerator.php
+++ b/src/Generators/MigrationGenerator.php
@@ -94,6 +94,16 @@ protected function populateStub(string $stub, Model $model): string
             $stub = $this->disableForeignKeyConstraints($stub);
         }
 
+        if ($model->usesCustomDatabaseConnection()) {
+            $property = str_replace(
+                ['{{ name }}', 'by the model.'],
+                [$model->databaseConnection(), 'by the migration.'],
+                $this->filesystem->stub('model.connection.stub')
+            );
+
+            $stub = Str::replaceFirst('{', '{' . PHP_EOL . $property, $stub);
+        }
+
         return $stub;
     }
 
diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php
index 21c78297..9f1e38e8 100644
--- a/src/Generators/ModelGenerator.php
+++ b/src/Generators/ModelGenerator.php
@@ -171,6 +171,10 @@ protected function buildProperties(Model $model): string
     {
         $properties = [];
 
+        if ($model->usesCustomDatabaseConnection()) {
+            $properties[] = str_replace('{{ name }}', $model->databaseConnection(), $this->filesystem->stub('model.connection.stub'));
+        }
+
         if ($model->usesCustomTableName() || $model->isPivot()) {
             $properties[] = str_replace('{{ name }}', $model->tableName(), $this->filesystem->stub('model.table.stub'));
         }
diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php
index f4d2f268..ffab3b0b 100644
--- a/src/Lexers/ModelLexer.php
+++ b/src/Lexers/ModelLexer.php
@@ -132,6 +132,10 @@ private function buildModel(string $name, array $columns): Model
         $model = new Model($name);
 
         if (isset($columns['meta']) && is_array($columns['meta'])) {
+            if (isset($columns['meta']['connection'])) {
+                $model->setDatabaseConnection($columns['meta']['connection']);
+            }
+
             if (isset($columns['meta']['table'])) {
                 $model->setTableName($columns['meta']['table']);
             }
diff --git a/src/Models/Model.php b/src/Models/Model.php
index d4703b81..6ffb29e9 100644
--- a/src/Models/Model.php
+++ b/src/Models/Model.php
@@ -19,6 +19,8 @@ class Model implements BlueprintModel
 
     private string|bool $softDeletes = false;
 
+    private ?string $connection;
+
     private string $table;
 
     private array $columns = [];
@@ -124,6 +126,21 @@ public function setPivot(): void
         $this->pivot = true;
     }
 
+    public function usesCustomDatabaseConnection(): bool
+    {
+        return isset($this->connection);
+    }
+
+    public function databaseConnection(): ?string
+    {
+        return $this->connection;
+    }
+
+    public function setDatabaseConnection(string $connection)
+    {
+        $this->connection = $connection;
+    }
+
     public function usesCustomTableName(): bool
     {
         return isset($this->table);
diff --git a/stubs/model.connection.stub b/stubs/model.connection.stub
new file mode 100644
index 00000000..5e3adb13
--- /dev/null
+++ b/stubs/model.connection.stub
@@ -0,0 +1,6 @@
+    /**
+     * The database connection that should be used by the model.
+     *
+     * @var string
+     */
+    protected $connection = '{{ name }}';


Test patch:
diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php
index c4330475..807036a2 100644
--- a/tests/Feature/Generators/MigrationGeneratorTest.php
+++ b/tests/Feature/Generators/MigrationGeneratorTest.php
@@ -54,6 +54,12 @@ public function output_generates_migrations($definition, $path, $model): void
             ->with('migration.stub')
             ->andReturn($this->stub('migration.stub'));
 
+        if ($definition === 'drafts/model-with-meta.yaml') {
+            $this->filesystem->expects('stub')
+                ->with('model.connection.stub')
+                ->andReturn($this->stub('model.connection.stub'));
+        }
+
         $now = Carbon::now();
         Carbon::setTestNow($now);
 
diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php
index 52f88a17..2a4fedac 100644
--- a/tests/Feature/Generators/ModelGeneratorTest.php
+++ b/tests/Feature/Generators/ModelGeneratorTest.php
@@ -60,6 +60,10 @@ public function output_generates_models($definition, $path, $model): void
         }
 
         if ($definition === 'drafts/model-with-meta.yaml') {
+            $this->filesystem->expects('stub')
+                ->with('model.connection.stub')
+                ->andReturn($this->stub('model.connection.stub'));
+
             $this->filesystem->expects('stub')
                 ->with('model.table.stub')
                 ->andReturn($this->stub('model.table.stub'));
diff --git a/tests/fixtures/drafts/model-with-meta.yaml b/tests/fixtures/drafts/model-with-meta.yaml
index a615cf2f..5b6c33bb 100644
--- a/tests/fixtures/drafts/model-with-meta.yaml
+++ b/tests/fixtures/drafts/model-with-meta.yaml
@@ -1,6 +1,7 @@
 models:
   Post:
     meta:
+      connection: read-only
       pivot: true
       table: post
     title: string:400
diff --git a/tests/fixtures/migrations/model-with-meta.php b/tests/fixtures/migrations/model-with-meta.php
index 421baff6..2a67bf65 100644
--- a/tests/fixtures/migrations/model-with-meta.php
+++ b/tests/fixtures/migrations/model-with-meta.php
@@ -6,6 +6,13 @@
 
 return new class extends Migration
 {
+    /**
+     * The database connection that should be used by the migration.
+     *
+     * @var string
+     */
+    protected $connection = 'read-only';
+
     /**
      * Run the migrations.
      */
diff --git a/tests/fixtures/models/model-with-meta.php b/tests/fixtures/models/model-with-meta.php
index 70276c97..4697e3e4 100644
--- a/tests/fixtures/models/model-with-meta.php
+++ b/tests/fixtures/models/model-with-meta.php
@@ -9,6 +9,13 @@ class Post extends Pivot
 {
     use HasFactory;
 
+    /**
+     * The database connection that should be used by the model.
+     *
+     * @var string
+     */
+    protected $connection = 'read-only';
+
     /**
      * The table associated with the model.
      *