Laravel Scout Search Configuration
Configuration guide for Laravel Scout search, including searching joined columns from related tables.Basic Configuration
Note: With the database Scout driver, no indexing happens by default. Only include actual database columns intoSearchableArray(), not computed values unless they’re real columns (likecomputed_text_id).
Searching Joined Columns
For Search AND Display
OverrideloadDomainObject() when you need the joined data for both search and display purposes:
Note: UsejoinRelationship()to automatically join related tables. Use table-prefixed keys (e.g.,'users.name') intoSearchableArray()when referencing joined columns.
For Search ONLY
OverridesearchQueryConstraints() when you only need the joined data for search, not for display:
Search Behavior
Default: LIKE Matching
By default, all fields intoSearchableArray() use WHERE LIKE %search% OR ... (contains matching).
Prefix Search
Use#[SearchUsingPrefix] for fields that should match from the start (e.g., “123” matches “12345” but not “51234”):
Important: Numeric IDs cannot use prefix search. You must create a computed text column to enable prefix searching on IDs.
Full-Text Search
For better performance and advanced matching (tokenization, stemming, stop words) on long text columns, add a full-text index:Warning: Don’t use full-text indexes on short single-value fields (names, emails, SKUs) - they won’t match well. Full-text is for longer content where tokenization helps (descriptions, articles, comments).
Computed Text Columns for ID Search
Using Power Joins
ThejoinRelationship() method comes from the eloquent-power-joins package.
Common Patterns
Note: See the Power Joins documentation for more options.
Best Practices
- Choose the right method:
- Use
loadDomainObject()for joins needed in search AND display - Use
searchQueryConstraints()for joins only needed in search
- Use
- Use
joinRelationship(): Prefer->joinRelationship('user')over manual joins for cleaner code - Choose appropriate join type: Use
leftJoinRelationship()when the relationship might not exist - Always select base columns first:
->select('table_name.*', 'joined.column', ...) - Use table-prefixed keys: In
toSearchableArray(), use'users.name'when referencing joined columns - Use safe navigation:
$this->user?->nameinstead of$this->user->nameintoSearchableArray() - Index joined columns: Ensure foreign keys and joined columns have database indexes