diff --git a/MIGRATIONS.md b/MIGRATIONS.md index 995139b..e88d924 100644 --- a/MIGRATIONS.md +++ b/MIGRATIONS.md @@ -85,6 +85,7 @@ All customizable models can be configured with additional fillable, guarded, hid Customize the model using the available static properties: +- `$useConnection`: The connection name to use. - `$useCasts`: The casts attributes to merge. - `$useFillable`: The fillable attributes to merge. - `$useGuarded`: The guarded attributes to merge. diff --git a/README.md b/README.md index 46f3e05..9af1fb5 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ class MyModel extends Model From there, the end-developer can customize the model using the available static properties: -- `$useTable`: A custom table name to use. +- `$useConnection`: The custom connection name to use. +- `$useTable`: The custom table name to use. - `$useCasts`: The casts attributes to merge. - `$useFillable`: The fillable attributes to merge. - `$useGuarded`: The guarded attributes to merge. diff --git a/src/CustomizableModel.php b/src/CustomizableModel.php index a6fb35c..4d019f2 100644 --- a/src/CustomizableModel.php +++ b/src/CustomizableModel.php @@ -14,6 +14,13 @@ */ trait CustomizableModel { + /** + * The connection name to use with the model. + * + * @var string|null + */ + public static $useConnection; + /** * The table name to use for this customizable model. * @@ -76,6 +83,8 @@ protected static function bootCustomizableModel(): void */ protected function initializeCustomizableModel(): void { + $this->connection = static::$useConnection; + $this->table = static::$useTable; $resolve = static function (Closure|array $value, $model): array { diff --git a/tests/CustomizableModelTest.php b/tests/CustomizableModelTest.php index c37d312..7642a54 100644 --- a/tests/CustomizableModelTest.php +++ b/tests/CustomizableModelTest.php @@ -10,20 +10,34 @@ class CustomizableModelTest extends TestCase { protected function setUp(): void { + TestModel::$useConnection = null; TestModel::$useTable = null; TestModel::$useCasts = []; TestModel::$useFillable = []; TestModel::$useGuarded = []; TestModel::$useHidden = []; TestModel::$useVisible = []; + TestModel::$useAppends = []; } #[Test] - public function uses_default_table_name(): void + public function uses_default_connection(): void + { + static::assertNull((new TestModel())->getConnectionName()); + } + + #[Test] + public function uses_custom_connection(): void { - TestModel::$useTable = 'foo'; + TestModel::$useConnection = 'foo'; - static::assertSame('foo', (new TestModel())->getTable()); + static::assertSame('foo', (new TestModel())->getConnectionName()); + } + + #[Test] + public function uses_default_table_name(): void + { + static::assertSame('test_models', (new TestModel())->getTable()); } #[Test]