PHPackages                             eelcol/laravel-meilisearch - 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. [API Development](/categories/api)
4. /
5. eelcol/laravel-meilisearch

ActiveLibrary[API Development](/categories/api)

eelcol/laravel-meilisearch
==========================

Simple Laravel wrapper to use Meilisearch

2.3.7(2mo ago)1412.9k↓12.1%5[2 issues](https://github.com/eelcol/laravel-meilisearch/issues)MITPHPPHP ^8.1

Since Mar 24Pushed 4d ago2 watchersCompare

[ Source](https://github.com/eelcol/laravel-meilisearch)[ Packagist](https://packagist.org/packages/eelcol/laravel-meilisearch)[ RSS](/packages/eelcol-laravel-meilisearch/feed)WikiDiscussions v2.x Synced 3w ago

READMEChangelogDependencies (10)Versions (42)Used By (0)

Laravel Meilisearch with QueryBuilder
=====================================

[](#laravel-meilisearch-with-querybuilder)

When you want to use Meilisearch in your Laravel application, you can use Laravel Scout. This is an easy way to sync your models to Meilisearch and quickly search models using Meilisearch. However, sometimes Laravel Scout is not enough. For example if you want:

- More control over your Meilisearch database: do not only save models for example.
- Set searchable, filterable or sortable attributes.
- Perform more complex queries to Meilisearch, for example with multiple filters.
- Use a query builder to fetch data from the Meilisearch database.
- To use some functionalities that are not available in Meilisearch out-of-the-box. For example, displaying documents in random order or displaying facets that are not available in the filtered documents.

This package deals with these kind of situations. You decide which information to send to Meilisearch, and which information you want back. The query builder specifically built for Meilisearch helps to build more complex queries.

When using this package, you should determine yourself when and which data you sent to Meilisearch. So if you automatically want to sent models to Meilisearch after a model is saved or created, Laravel Scout might be a better solution.

Compatibility with Meilisearch
------------------------------

[](#compatibility-with-meilisearch)

Currently, this package supports Meilisearch up to version 0.27. Version 0.28 of Meilisearch introduced some breaking changes. A new version of this package compatible with 0.28 will be released soon.

Installation
------------

[](#installation)

Determine which version you need based on the Meilisearch version:

Meilisearch versionPackage versionUp to 0.27&lt;=10.28^1.00.30~2.0.01.0.\*~2.0.01.1.\*~2.1.01.2.\*~2.2.01.3.\*~2.3.0So for example, when using Meilisearch 1.0.2, use the following command:

```
composer require eelcol/laravel-meilisearch:~2.0.0

```

### Setup .env

[](#setup-env)

Change your .env to include the following variables:

```
MEILISEARCH_HOST=...
MEILISEARCH_KEY=...

```

When not using a Meilisearch key, the .env variable MEILISEARCH\_KEY can be any value.

### Publish assets

[](#publish-assets)

```
php artisan vendor:publish --tag=laravel-meilisearch

```

Getting started
---------------

[](#getting-started)

### Create an index

[](#create-an-index)

First you need to create an index to save documents to. For example, you need an index to save our products catalogue to. So the following command can be used:

```
php artisan meilisearch:create-index products

```

This command will create a file `database/meilisearch/products.php`. In this file, you can adjust settings for this index. This is not required, however it is highly recommended. If you leave the standard settings, Meilisearch will use all columns of your data to search on. To achieve this, Meilisearch must index all columns of your data. This will take a longer time, and uses more server resources. That's why it is recommended to specify which columns should be searchable, filterable and sortable.

Everytime you want to change something to the settings, simply change this file. After the changes, run the command below.

### Migrate the index to the Meilisearch database

[](#migrate-the-index-to-the-meilisearch-database)

Now the index has to be actually created. To achieve this, run the following command:

```
php artisan meilisearch:set-index-settings

```

Compare this to the database migrations of Laravel. First you have to create a database migration, next you have to run the migration to actually create the table, or make the adjustment.

Run this command *every time you make changes to the `database/meilisearch/products.php` file*. Also, run this command *on every deployment*, so you have an up-to-date Meilisearch instance in production.

When you want to set the index settings on a different Meilisearch installation, you can use the `--mshost` and `--mskey` options:

```
php artisan meilisearch:set-index-settings --mshost=http://another-meilisearch-installation:7700 --mskey=secret-key

```

Master data in Meilisearch
--------------------------

[](#master-data-in-meilisearch)

All the functionalities that are mentioned in the Meilisearch docs are available in this package. The most important functionalities are listed below:

### Insert data

[](#insert-data)

To insert a document in the index `products`, you can do 1 of the following:

```
Meilisearch::addDocument('products', [
    'id' => 1,
    'title' => 'iPhone SE'
]);
```

```
Meilisearch::addDocuments('products', [
    [
        'id' => 1,
        'title' => 'iPhone SE'
    ],
    [
        'id' => 2,
        'title' => 'Samsung Galaxy'
    ]
]);
```

You can also directly insert a model or collection. A model gets converted to an array. In order to do this, the package check if the following methods exists on the object, in the following order:

```
- toMeilisearch()
- toSearchableArray()
- toArray()
```

A `Product` model for example, can look like the following:

```
