Skip to content

Commit

Permalink
HasSettingsField now adheres to $connection override on model (#68)
Browse files Browse the repository at this point in the history
* FIX: HasSettingsField now adheres to $connection override on model (#62) (#64)

* Refactor

* FIX: HasSettingsField now adheres to $connection override on model

Co-authored-by: Gombos Lorand <[email protected]>

* Update CHANGELOG

* Refactor

Co-authored-by: Bryan Jones <[email protected]>
  • Loading branch information
glorand and bickle66 authored Sep 10, 2020
1 parent e16416f commit 1e65f14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `glorand/laravel-model-settings` will be documented in this file

## 3.7.0 - 2020-09-10
### Added
- HasSettingsField now adheres to $connection override on model [Ref. task](https://github.com/glorand/laravel-model-settings/issues/62)

## 3.6.7 - 2020-08-27
### Fix
- code refactor
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<a title="MadeWithLaravel.com Shield" href="https://madewithlaravel.com/p/laravel-model-settings/shield-link"> <img src="https://madewithlaravel.com/storage/repo-shields/1716-shield.svg"/></a>
</p>

The package requires PHP 7.1.3+ and follows the FIG standards PSR-1, PSR-2 and PSR-4
The package requires PHP 7.1.3+ and follows the FIG standards PSR-1, PSR-2 and PSR-4
to ensure a high level of interoperability between shared PHP.

Bug reports, feature requests, and pull requests can be submitted by following our [Contribution Guide](CONTRIBUTING.md).
Expand Down Expand Up @@ -106,9 +106,12 @@ use Glorand\Model\Settings\Traits\HasSettingsField;
class User extends Model
{
use HasSettingsField;

//define only if you select a different name from the default
public $settingsFieldName = 'user_settings';
public $settingsFieldName = 'user_settings';

//define only if the model overrides the default connection
protected $connection = 'mysql';

}
```
Expand Down Expand Up @@ -137,14 +140,14 @@ class User extends Model

## Default settings <a name="default_settings"></a>

You can set default configs for a table in model_settings.php config file
You can set default configs for a table in model_settings.php config file

```php
return [
// start other config options

// end other config options

// defaultConfigs
'defaultSettings' => [
'users' => [
Expand All @@ -167,7 +170,7 @@ class User extends Model
}
```

> Please note that if you define settings in the model, the settings from configs will have no effect, they will just be ignored.
> Please note that if you define settings in the model, the settings from configs will have no effect, they will just be ignored.

## Usage <a name="usage"></a>
Expand Down
14 changes: 13 additions & 1 deletion src/Traits/HasSettingsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public function getSettingsFieldName(): string
return $this->settingsFieldName ?? config('model_settings.settings_field_name');
}

/**
* @return string
*/
public function getConnectionName(): string
{
return $this->connection ?? config('database.default');
}

/**
* @return bool
*/
Expand All @@ -100,7 +108,11 @@ private function hasSettingsField()
config('model_settings.settings_table_cache_prefix') . '::has_field',
now()->addDays(1),
function () {
return Schema::hasColumn($this->getTable(), $this->getSettingsFieldName());
return Schema::connection($this->getConnectionName())
->hasColumn(
$this->getTable(),
$this->getSettingsFieldName()
);
}
);
}
Expand Down

0 comments on commit 1e65f14

Please sign in to comment.