Skip to content

Commit fbe92da

Browse files
add support for defining the database connection table
1 parent 9030a02 commit fbe92da

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-settings` will be documented in this file
44

5+
## 2.1.6 - 2021-06-03
6+
7+
- add support for defining the database connection table
8+
59
## 2.1.5 - 2021-05-21
610

711
- fix some casting problems

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ return [
119119
'database' => [
120120
'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class,
121121
'model' => null,
122+
'table' => null,
122123
'connection' => null,
123124
],
124125
'redis' => [
@@ -931,6 +932,7 @@ There are two types of repositories included in the package, the `redis` and `da
931932
The database repository has two optional configuration options:
932933

933934
- `model` the Eloquent model used to load/save properties to the database
935+
- `table` the table used in the database
934936
- `connection` the connection to use when interacting with the database
935937

936938
#### Redis repository

config/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'database' => [
3030
'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class,
3131
'model' => null,
32+
'table' => null,
3233
'connection' => null,
3334
],
3435
'redis' => [

src/SettingsRepositories/DatabaseSettingsRepository.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,28 @@
22

33
namespace Spatie\LaravelSettings\SettingsRepositories;
44

5-
use DB;
5+
use Illuminate\Database\Eloquent\Builder;
66
use Spatie\LaravelSettings\Models\SettingsProperty;
77

88
class DatabaseSettingsRepository implements SettingsRepository
99
{
10-
/** @var string|\Illuminate\Database\Eloquent\Model */
10+
/** @var class-string<\Illuminate\Database\Eloquent\Model> */
1111
protected string $propertyModel;
1212

1313
protected ?string $connection;
1414

15+
protected ?string $table;
16+
1517
public function __construct(array $config)
1618
{
1719
$this->propertyModel = $config['model'] ?? SettingsProperty::class;
18-
1920
$this->connection = $config['connection'] ?? null;
21+
$this->table = $config['table'] ?? null;
2022
}
2123

2224
public function getPropertiesInGroup(string $group): array
2325
{
24-
/**
25-
* @var \Spatie\LaravelSettings\Models\SettingsProperty $temp
26-
* @psalm-suppress UndefinedClass
27-
*/
28-
$temp = new $this->propertyModel();
29-
30-
return DB::connection($this->connection ?? $temp->getConnectionName())
31-
->table($temp->getTable())
26+
return $this->getBuilder()
3227
->where('group', $group)
3328
->get(['name', 'payload'])
3429
->mapWithKeys(function ($object) {
@@ -39,15 +34,15 @@ public function getPropertiesInGroup(string $group): array
3934

4035
public function checkIfPropertyExists(string $group, string $name): bool
4136
{
42-
return $this->propertyModel::on($this->connection)
37+
return $this->getBuilder()
4338
->where('group', $group)
4439
->where('name', $name)
4540
->exists();
4641
}
4742

4843
public function getPropertyPayload(string $group, string $name)
4944
{
50-
$setting = $this->propertyModel::on($this->connection)
45+
$setting = $this->getBuilder()
5146
->where('group', $group)
5247
->where('name', $name)
5348
->first('payload')
@@ -58,7 +53,7 @@ public function getPropertyPayload(string $group, string $name)
5853

5954
public function createProperty(string $group, string $name, $payload): void
6055
{
61-
$this->propertyModel::on($this->connection)->create([
56+
$this->getBuilder()->create([
6257
'group' => $group,
6358
'name' => $name,
6459
'payload' => json_encode($payload),
@@ -68,7 +63,7 @@ public function createProperty(string $group, string $name, $payload): void
6863

6964
public function updatePropertyPayload(string $group, string $name, $value): void
7065
{
71-
$this->propertyModel::on($this->connection)
66+
$this->getBuilder()
7267
->where('group', $group)
7368
->where('name', $name)
7469
->update([
@@ -78,34 +73,49 @@ public function updatePropertyPayload(string $group, string $name, $value): void
7873

7974
public function deleteProperty(string $group, string $name): void
8075
{
81-
$this->propertyModel::on($this->connection)
76+
$this->getBuilder()
8277
->where('group', $group)
8378
->where('name', $name)
8479
->delete();
8580
}
8681

8782
public function lockProperties(string $group, array $properties): void
8883
{
89-
$this->propertyModel::on($this->connection)
84+
$this->getBuilder()
9085
->where('group', $group)
9186
->whereIn('name', $properties)
9287
->update(['locked' => true]);
9388
}
9489

9590
public function unlockProperties(string $group, array $properties): void
9691
{
97-
$this->propertyModel::on($this->connection)
92+
$this->getBuilder()
9893
->where('group', $group)
9994
->whereIn('name', $properties)
10095
->update(['locked' => false]);
10196
}
10297

10398
public function getLockedProperties(string $group): array
10499
{
105-
return $this->propertyModel::on($this->connection)
100+
return $this->getBuilder()
106101
->where('group', $group)
107102
->where('locked', true)
108103
->pluck('name')
109104
->toArray();
110105
}
106+
107+
public function getBuilder(): Builder
108+
{
109+
$model = new $this->propertyModel;
110+
111+
if($this->connection){
112+
$model->setConnection($this->connection);
113+
}
114+
115+
if($this->table){
116+
$model->setTable($this->table);
117+
}
118+
119+
return $model->newQuery();
120+
}
111121
}

tests/SettingsRepositories/DatabaseSettingsRepositoryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\LaravelSettings\Tests\SettingsRepositories;
44

55
use Closure;
6+
use DB;
67
use Illuminate\Database\Schema\Blueprint;
78
use Illuminate\Support\Facades\Schema;
89
use Spatie\LaravelSettings\Models\SettingsProperty;
@@ -258,6 +259,30 @@ public function it_can_have_different_configuration_options(Closure $repositoryF
258259
], $otherRepository->getPropertiesInGroup('test'));
259260
}
260261

262+
/** @test */
263+
public function it_can_have_a_different_table_name()
264+
{
265+
Schema::create('spatie_settings', function (Blueprint $table): void {
266+
$table->id();
267+
268+
$table->string('group')->index();
269+
$table->string('name');
270+
$table->boolean('locked');
271+
$table->json('payload');
272+
273+
$table->timestamps();
274+
});
275+
276+
$repository = new DatabaseSettingsRepository([
277+
'table' => 'spatie_settings'
278+
]);
279+
280+
$repository->createProperty('test', 'a', 'Alpha');
281+
282+
$this->assertEquals(1, DB::table('spatie_settings')->count());
283+
$this->assertEquals(0, DB::table('settings')->count());
284+
}
285+
261286
public function configurationsProvider(): array
262287
{
263288
return [

0 commit comments

Comments
 (0)