diff --git a/README.md b/README.md index 26a2905..76c9855 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Schema::table('your_models', function (Blueprint $table) { ### Preparing the model -In order to work with the schemaless attributes you'll need to add a cast, an accessor and a scope on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name. +In order to work with the schemaless attributes you'll need to add a cast, an accessor and a scopes on your model. Here's an example of what you need to add if you've chosen `extra_attributes` as your column name. ```php use Illuminate\Database\Eloquent\Model; @@ -106,6 +106,43 @@ class TestModel extends Model } ``` +If you need support for multiple schemaless columns on a single model, you should use `SchemalessAttributesTrait` trait. Here's an example of what you need to add if you've chosen `extra_attributes, other_extra_attributes` as your column names. + +```php +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use Spatie\SchemalessAttributes\SchemalessAttributes; +use Spatie\SchemalessAttributes\SchemalessAttributesTrait; + +class TestModel extends Model +{ + use SchemalessAttributesTrait; + + // ... + + /** + * @var array + */ + protected $schemalessAttributes = [ + 'extra_attributes', + 'other_extra_attributes', + ]; + + public function scopeWithExtraAttributes(): Builder + { + return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes'); + } + + public function scopeWithOtherExtraAttributes(): Builder + { + return SchemalessAttributes::scopeWithSchemalessAttributes('other_extra_attributes'); + } + + // ... +} +``` + + If you want to reuse this behaviour across multiple models you could opt to put the function in a trait of your own. Here's what that trait could look like: ```php diff --git a/src/SchemalessAttributesTrait.php b/src/SchemalessAttributesTrait.php new file mode 100644 index 0000000..c935554 --- /dev/null +++ b/src/SchemalessAttributesTrait.php @@ -0,0 +1,38 @@ +getSchemalessAttributes() as $attribute) { + $this->casts[$attribute] = 'array'; + } + } + + public function getSchemalessAttributes(): array + { + return isset($this->schemalessAttributes) ? $this->schemalessAttributes : []; + } + + /** + * @param $key + * @return mixed|SchemalessAttributes + */ + public function __get($key) + { + if (in_array($key, $this->getSchemalessAttributes())) { + return SchemalessAttributes::createForModel($this, $key); + } + + return parent::__get($key); + } +} diff --git a/tests/SchemalessAttributesTraitTest.php b/tests/SchemalessAttributesTraitTest.php new file mode 100644 index 0000000..be26b0d --- /dev/null +++ b/tests/SchemalessAttributesTraitTest.php @@ -0,0 +1,56 @@ +testModel = new TestModelUsedTrait(); + } + + /** @test */ + public function schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() + { + if (Str::startsWith(app()->version(), '5.6')) { + $this->assertFalse($this->testModel->hasCast('schemaless_attributes', 'array')); + } else { + $this->assertTrue($this->testModel->hasCast('schemaless_attributes', 'array')); + } + } + + /** @test */ + public function other_schemaless_attributes_cast_as_array_initialize_schemaless_attributes_trait() + { + if (Str::startsWith(app()->version(), '5.6')) { + $this->assertFalse($this->testModel->hasCast('other_schemaless_attributes', 'array')); + } else { + $this->assertTrue($this->testModel->hasCast('other_schemaless_attributes', 'array')); + } + } + + /** @test */ + public function test_get_schemaless_attributes() + { + $this->assertEquals(['schemaless_attributes', 'other_schemaless_attributes'], $this->testModel->getSchemalessAttributes()); + } + + /** @test */ + public function test_get() + { + $this->assertNull($this->testModel->schemaless_attributes->name); + } + + /** @test */ + public function test_parent_get() + { + $this->assertNull($this->testModel->not_existing_schemaless); + } +} diff --git a/tests/TestModelUsedTrait.php b/tests/TestModelUsedTrait.php new file mode 100644 index 0000000..28ae019 --- /dev/null +++ b/tests/TestModelUsedTrait.php @@ -0,0 +1,18 @@ +