Skip to content

Commit

Permalink
Merge pull request #3 from Laragear/feat/connection
Browse files Browse the repository at this point in the history
[1.x] Adds support for custom connections
  • Loading branch information
DarkGhostHunter authored Mar 15, 2024
2 parents d8f7aa0 + 8da476f commit 3f733e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class Car 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.
Expand Down
9 changes: 9 additions & 0 deletions src/CustomizableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 17 additions & 3 deletions tests/CustomizableModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 3f733e7

Please sign in to comment.