nourayman / search-json
Search JSON columns with Arabic character normalization
Requires
- php: >=7.4
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Overview
The SearchJson
package provides robust search functionality for JSON fields within a Laravel application. It allows you to search for terms in multiple languages across different JSON fields, while also supporting flexible regex patterns to handle Arabic character variations.
Features
- Search within JSON fields across multiple languages (e.g.,
ar
,en
). - Supports character normalization for Arabic, so variations of characters like
ا
,أ
,إ
, andآ
are all matched.
Installation
To install the SearchJson package, simply run the following command:
composer require nourayman/search-json
Usage
- Add Searchable Trait to your Model
In your Model, Add traitSearhable
to Article Model:
use Nourayman\SearchJson\Searchable; class Article { use Searchable; // Your Model Code }
In your Controller, use the function searchJson
directly:
$field = 'title'; $langs = ['ar', 'en']; $text = 'ألنفسيه'; $results = Article::searchJson($field, $text, $langs);
- Customizing Character Variations
The package automatically handles Arabic character normalization. You can further customize this by modifying the buildRegexPattern
method in the service class to add new character variations or optional prefixes as needed.
Methods
searchJson($field, $term, array $langs)
- Description: Searches through specified JSON fields in multiple languages, handling variations in Arabic characters.
- Parameters:
$field
: THe JSON field to search within (e.g.,title
).$term
: The search term, which can include any form of Arabic characters.$langs
: Languages to search within the JSON fields (e.g.,['ar', 'en']
).
- Returns: Collection of matched results.
Example Query
For a given term like 'ألنفسيه'
, the package will match:
النفسية
ألنفسية
إلنفسية
النفسيه
ألنفسيه
إلنفسية
This search flexibility is ideal for applications needing robust multi-language support in JSON fields.
Contributing
Contributions are welcome! If you have any bug reports, feature requests, or pull requests, please submit them to the GitHub repository.
License
This package is open-sourced software licensed under the MIT license.