- How do I install the Meilisearch PHP client in a Laravel project?
- Use Composer to install the package with `composer require meilisearch/meilisearch-php`. Laravel’s built-in HTTP client (Symfony HTTP Client) is supported by default, so no additional setup is needed unless you’re using a custom HTTP client like Guzzle. The package integrates cleanly with Laravel’s service container.
- Does this package support Laravel’s Eloquent models for search?
- Yes, you can extend Eloquent models with a custom `search()` scope or accessor that uses the Meilisearch client. For example, add a `scopeSearch()` method to your model to query Meilisearch indexes directly. This allows you to mix Meilisearch results with Eloquent queries seamlessly.
- What Laravel versions are compatible with this Meilisearch PHP package?
- The package is compatible with Laravel 8.x, 9.x, and 10.x. It relies on PHP 8.0+, which aligns with Laravel’s minimum requirements. Always pin the package version in `composer.json` to avoid unexpected updates, especially if you’re using Meilisearch’s v1.x LTS.
- How do I handle real-time updates when a model changes in Laravel?
- Use Laravel’s event system or queues to trigger Meilisearch updates asynchronously. For example, listen to `ModelUpdated` events and push changes to Meilisearch using `updateDocuments()` or `addDocuments()`. This ensures search indexes stay in sync without blocking your application.
- Can I use Meilisearch as a fallback for SQL search queries in Laravel?
- Yes, you can implement a hybrid approach where Meilisearch handles search-heavy queries (e.g., product catalogs) while falling back to SQL for simpler queries. Use Laravel’s feature flags or middleware to toggle between the two based on query complexity or performance needs.
- How do I configure Meilisearch for typo-tolerant searches in Laravel?
- Meilisearch’s typo-tolerance is enabled by default, but you can fine-tune it via the client’s `updateSettings()` method. For example, adjust `typoTolerance` or `stopWords` in your index settings. This works out of the box with the PHP client and requires no additional Laravel configuration.
- What’s the best way to test Meilisearch integration in Laravel’s PHPUnit tests?
- Mock the Meilisearch client using PHPUnit’s HTTP interceptors or a test container. The package’s documentation includes examples for testing search queries and index updates. Alternatively, use Laravel’s `Http` facade to stub responses for Meilisearch API calls during testing.
- How do I handle pagination with Meilisearch in Laravel?
- Meilisearch returns results with `offset` and `limit` parameters, which can be mapped to Laravel’s paginator. Use the `getHits()` method to fetch results and manually paginate them in your controller or service layer. For seamless integration, create a custom paginator class that extends Laravel’s `LengthAwarePaginator`.
- Is Meilisearch Cloud supported, and how do I connect to it?
- Yes, the package supports Meilisearch Cloud out of the box. Simply configure the client with your Cloud instance’s URL and API key, just like with a self-hosted instance. The connection process is identical, and you can switch between self-hosted and Cloud environments by updating your `.env` file.
- What are the alternatives to this package for Laravel search, and when should I consider them?
- Alternatives include Laravel Scout with Algolia or Elasticsearch, or direct database full-text search (PostgreSQL/MySQL). Use Meilisearch PHP when you need a lightweight, open-source solution with typo tolerance and faceted search. Scout is better for managed services, while Elasticsearch offers more advanced features but with higher complexity.