22
33namespace Spatie \LaravelSettings \SettingsRepositories ;
44
5- use DB ;
5+ use Illuminate \ Database \ Eloquent \ Builder ;
66use Spatie \LaravelSettings \Models \SettingsProperty ;
77
88class 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}
0 commit comments