PHPackages                             guestpectacular/melastic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. guestpectacular/melastic

ActiveLibrary[Search &amp; Filtering](/categories/search)

guestpectacular/melastic
========================

Meilisearch + Eloquent

v1.1.0(1mo ago)0117↓91.7%MITPHPPHP ^8.3

Since Mar 14Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/guestpectacular/melastic)[ Packagist](https://packagist.org/packages/guestpectacular/melastic)[ Docs](https://github.com/guestpectacular/melastic)[ RSS](/packages/guestpectacular-melastic/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (18)Versions (3)Used By (0)

Melastic: Meilisearch + Eloquent
================================

[](#melastic-meilisearch--eloquent)

Melastic: Eloquent Query Builder + Meilisearch.

Melastic it's an extension that extend [Laravel Scout](https://laravel.com/docs/10.x/scout) package to implement missing [Eloquent Builder](https://laravel.com/docs/10.x/queries) capabilities using Elasticsearch with [Meilisearch](https://www.meilisearch.com/).

Introduction
============

[](#introduction)

Laravel Scout provides a simple, driver based solution for adding full-text search to your Eloquent models Using model observers. Laravel Scout only supports simple filters only using AND boolean between expressions by default, Melastic allows for more flexible queries that support `AND`, `OR`, and combined multiple scopes using nested closures.

Improvements over Laravel Scout
===============================

[](#improvements-over-laravel-scout)

In this guide you will see how to configure and use Meilisearch filters in a hypothetical movie database.

Laravel Scout current state supporting only limiting to use `AND` operator:

```
// Search through users filterable attributes looking for "Pimienta", and applying filters to look where age = 38 AND where name = 'Edgar'
User::search('Pimienta')->where('age', 38)->where('name', 'Edgar')->get();
```

If we need to implement a custom filter using OR we need to write a raw expression using this approach:

```
User::search('Pimienta', function ($engine, $query, $options) {
   $options['filter'] = "age = 38 OR name = 'Edgar'";
   // ... extra filters
   return $meilisearch->search($query, $options);
})->get()
```

What about more custom filters?
-------------------------------

[](#what-about-more-custom-filters)

```
$ids	=	[1,2,3];
User::search("Pimienta", function ($engine, $query, $options) use ($ids) {
   $options['filter'] = "(age = 38 OR name = 'Edgar') OR id NOT IN [" . implode(', ', $ids) . "]";
   return $meilisearch->search($query, $options);
})->get()
```

IMHO It feels kinda bloated and difficult to maintain using different dynamic filters.

Difference with Melastic
========================

[](#difference-with-melastic)

With Melastic, it is now possible to filter using natural Eloquent Builder methods, let's rewrite same example using Laravel style:

```
User::search('Pimienta')
->where(function ($query) {
    $query->where('age', 38)
          ->orWhere('name', 'Edgar');
})
->orWhereNotIn('id',[1,2,3])
->get();
```

I have ported all missing functions from `Illuminate\Database\Query\Builder` into `Laravel\Scout\Builder`. These include `where`, `orWhere`, `whereIn`, `orWhereIn`, `whereNotIn`, `orWhereNotIn`, `whereExists`, `orWhereExists`, `whereNotExists`, `whereBetween`, `whereNull`, `orWhereNull`, `whereNotNull`, `whereIsEmpty`, `orWhereIsEmpty`, `whereIsNotEmpty`, and `when` to use dynamic closures enabling us to practically use the same functionality as with Eloquent models.

Installation
============

[](#installation)

You can install the package via composer:

```
composer require guestpectacular/melastic
```

After installing Scout, you should publish the Scout configuration file using the `vendor:publish` Artisan command. This command will publish the `scout.php` configuration file to your application's `config` directory:

```
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
```

Update your config to use `Meilisearch`:

```
