diff --git a/src/Tenant.php b/src/Tenant.php index 4c9cb04d..b9d612d3 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -90,7 +90,7 @@ class Tenant extends Model public function put(string $key, $value) { - if (\array_key_exists($key, $this->customColumns())) { + if (\in_array($key, $this->customColumns())) { $this->update([$key => $value]); } else { $obj = \json_decode($this->{$this->dataColumn()}); diff --git a/tests/Etc/2019_08_08_000001_add_custom_column.php b/tests/Etc/2019_08_08_000001_add_custom_column.php new file mode 100644 index 00000000..5b5ac3f4 --- /dev/null +++ b/tests/Etc/2019_08_08_000001_add_custom_column.php @@ -0,0 +1,26 @@ +string('foo')->nullable(); + }); + } + + public function down() + { + } +} diff --git a/tests/TenantStorageTest.php b/tests/TenantStorageTest.php index 86230f1f..f5f2dcee 100644 --- a/tests/TenantStorageTest.php +++ b/tests/TenantStorageTest.php @@ -170,4 +170,32 @@ class TenantStorageTest extends TestCase $this->assertSame('bar', tenancy()->get('foo')); $this->assertSame(['bar'], tenancy()->get(['foo'])); } + + /** @test */ + public function custom_columns_work_with_db_storage_driver() + { + if (config('tenancy.storage_driver') != 'Stancl\Tenancy\StorageDrivers\DatabaseStorageDriver') { + $this->markTestSkipped(); + } + + tenancy()->end(); + + $this->loadMigrationsFrom([ + '--path' => __DIR__ . '/Etc', + '--database' => 'central', + ]); + config(['database.default' => 'sqlite']); // fix issue caused by loadMigrationsFrom + + config(['tenancy.storage.db.custom_columns' => [ + 'foo', + ]]); + + tenancy()->create('foo.localhost'); + tenancy()->init('foo.localhost'); + + tenancy()->put(['foo' => 'bar', 'abc' => 'xyz']); + $this->assertSame(['bar', 'xyz'], tenancy()->get(['foo', 'abc'])); + + $this->assertSame('bar', \DB::connection('central')->table('tenants')->where('uuid', tenant('uuid'))->first()->foo); + } }