Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for standalone index fields and query conditions on this fields #43

Merged
merged 2 commits into from
May 15, 2023

Conversation

Namoshek
Copy link
Owner

This PR adds support for standalone fields in the database which can be filtered on using the Builder::where($column, $value) method of the search builder.

To use this new feature, use the following steps:

  1. Add a migration for an additional column on the scout_index table:
return new class extends Migration {
    public function up(): void
    {
        Schema::table('scout_index', function (Blueprint $table) {
            $table->uuid('tenant_id');
        });
    }
    public function down(): void
    {
        Schema::table('scout_index', function (Blueprint $table) {
            $table->dropColumn(['tenant_id']);
        });
    }
};
  1. Index data for the new column by wrapping the value in StandaloneField:
class User extends Model
{
    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'tenant_id' => new StandaloneField($this->tenant_id),
        ];
    }
}
  1. Filter on the additional column using Builder::where():
User::search('Max Mustermann')
    ->where('tenant_id', $tenantId)
    ->get();

@sonarcloud
Copy link

sonarcloud bot commented May 12, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

100.0% 100.0% Coverage
1.1% 1.1% Duplication

@Namoshek Namoshek merged commit 2c24bf7 into master May 15, 2023
17 checks passed
@Namoshek Namoshek deleted the feature/additional-conditions branch May 15, 2023 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant