Skip to content

Commit

Permalink
Merge pull request #89 from erikn69/scope_with_operator
Browse files Browse the repository at this point in the history
Scope retrieving models with operator
  • Loading branch information
freekmurze authored Nov 8, 2021
2 parents ddc9e5f + 2d69725 commit f731d35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ If you only want to search on a single custom attribute, you can use the modelSc
$yourModel->withExtraAttributes('name', 'value')->get();
```

Also, if you only want to search on a single custom attribute with a custom operator, you can use the modelScope like this

```php
// returns all models that have a schemaless attribute `name` starting with `value`
$yourModel->withExtraAttributes('name', 'LIKE', 'value%')->get();
```

If you only want to search on a nested custom attribute, you can use the modelScope like this

```php
Expand Down
9 changes: 7 additions & 2 deletions src/SchemalessAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ public function modelScope(): Builder
[$builder, $schemalessAttributes] = $arguments;
}

if (count($arguments) >= 3) {
if (count($arguments) === 3) {
[$builder, $name, $value] = $arguments;
$schemalessAttributes = [$name => $value];
}

if (count($arguments) >= 4) {
[$builder, $name, $operator, $value] = $arguments;
$schemalessAttributes = [$name => $value];
}

foreach ($schemalessAttributes as $name => $value) {
$builder->where("{$this->sourceAttributeName}->{$name}", $value);
$builder->where("{$this->sourceAttributeName}->{$name}", $operator ?? '=', $value);
}

return $builder;
Expand Down
20 changes: 19 additions & 1 deletion tests/HasSchemalessAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,25 @@ public function it_has_a_scope_to_get_models_with_the_given_schemaless_attribute
$model1 = TestModel::create(['schemaless_attributes' => [
'name' => 'value',
'name2' => 'value2',
'arr' => [
'subKey1' => 'subVal1'
]
]]);

$model2 = TestModel::create(['schemaless_attributes' => [
'name' => 'value',
'name2' => 'value2',
'arr' => [
'subKey1' => 'subVal1'
]
]]);

$model3 = TestModel::create(['schemaless_attributes' => [
'name' => 'value',
'name2' => 'value3',
'arr' => [
'subKey1' => 'subVal2'
]
]]);

$this->assertContainsModels([
Expand All @@ -306,7 +315,16 @@ public function it_has_a_scope_to_get_models_with_the_given_schemaless_attribute
], TestModel::withSchemalessAttributes('name', 'value')->get());

$this->assertContainsModels([
], TestModel::withSchemalessAttributes('name', 'non-existing-value')->get());
$model1, $model2,
], TestModel::withSchemalessAttributes('name2', '!=', 'value3')->get());

$this->assertContainsModels([
$model3,
], TestModel::withSchemalessAttributes('arr->subKey1', 'subVal2')->get());

$this->assertContainsModels([
$model1, $model2,
], TestModel::withSchemalessAttributes('arr->subKey1', '!=', 'subVal2')->get());

$this->assertContainsModels([
], TestModel::withSchemalessAttributes('name', 'non-existing-value')->get());
Expand Down

0 comments on commit f731d35

Please sign in to comment.