PHPackages                             jdkweb/search - 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. jdkweb/search

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

jdkweb/search
=============

Laravel-Search is an engin using the models. Search easily, flexible add an intelligent on your Laravel website or application.

v2.1.2(11mo ago)140GPL-3.0-or-laterPHPPHP &gt;=8.1

Since Mar 21Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/jdkweb/laravel-search)[ Packagist](https://packagist.org/packages/jdkweb/search)[ RSS](/packages/jdkweb-search/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (16)Used By (0)

laravel Search
==============

[](#laravel-search)

[![Packagist Version](https://camo.githubusercontent.com/9c002d979b59b470caa1ec7481ea487414338cebe3e370ef8a44f25f4c8a6f3d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a646b7765622f736561726368)](https://packagist.org/packages/jdkweb/search)[![Static Badge](https://camo.githubusercontent.com/cecb38ec8d1565cf515e7fe38fe8bc8cd3a033291a7cf47f7026baf19d47fd65/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d253230253345253344253230382e312d627269676874677265656e3f7374796c653d666c6174266c6f676f3d706870266c6f676f53697a653d3136266c6162656c3d504850)](https://github.com/jdkweb/rdw-api-filament)[![Static Badge](https://camo.githubusercontent.com/16a05260506814f18580062bb392250d0d1a771682a747a27dbabb6df3fed5fa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d25323025334525334425323031302d736c617465677261793f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465266c6f676f53697a653d3136266c6162656c3d4c61726176656c)](https://github.com/jdkweb/rdw-api-filament)

Laravel-Search is a search-engine using the models. Search easily, flexible add intelligent on your Laravel website or application.

[![Static Badge](https://camo.githubusercontent.com/4ede22cdf4e763a6dfe58b3035d322a22055bb513eededfe462b53b7c1e1ddc6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d6a646b7765622532467365617263682d626c75653f7374796c653d666c61742d737175617265266c6f676f3d676974687562266c6162656c3d476974687562)](https://github.com/jdkweb/search)[![Static Badge](https://camo.githubusercontent.com/8221b0ffc4bfbd84bab4c62eb8c29b684eafc02b6515503fb9d6d4b9874b5f37/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d6a646b7765622532467365617263682d626c75653f7374796c653d666c61742d737175617265266c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465266c6162656c3d5061636b6167697374)](https://packagist.org/packages/jdkweb/search)

[![laravel search](https://camo.githubusercontent.com/d2972b9110034486d7228e08142c4c82a7be5e4b78e316ab74687780c9bc334c/68747470733a2f2f7777772e6a646b7765622e6e6c2f6769742f696d616765732f6c61726176656c2d7365617263682e77656270)](https://camo.githubusercontent.com/d2972b9110034486d7228e08142c4c82a7be5e4b78e316ab74687780c9bc334c/68747470733a2f2f7777772e6a646b7765622e6e6c2f6769742f696d616765732f6c61726176656c2d7365617263682e77656270)

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Configuration with config-file](#configuration-with-config-file)
        - [Search engine configuration](#search-engine-configuration)
            - [Searchable model configuration](#searchable-model-configuration)
            - [Example](#example)
        - [Rename query strings parameters](#rename-query-strings-parameters)
        - [Preset search words](#preset-search-words)
        - [Search Result items per page](#search-result-items-per-page)
        - [Example Config](#example-config)
    - [Configuration directly embed settings in script](#configuration-directly-embed-settings-in-script)
    - [Using the search engine](#using-the-search-engine)
        - [Filters (search groups)](#filters-search-groups)
- [Operators for conditions](#operators-for-conditions)
- [Filter specific words from the search](#filter-specific-words-from-the-search)
- [Methods and Closures](#methods-and-closures)
- [Compare configuration settings](#compare-configuration-setting)

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

[](#installation)

Requires PHP 8.1 and Laravel 10 or higher

Install the package via composer:

```
composer require jdkweb/laravel-search
```

### Config

[](#config)

For configuration settings you need to publish the config

```
php artisan vendor:publish --provider="Jdkweb\Search\SearchServiceProvider" --tag="config"
```

In the config is needed for:

- To setup reusable independent search-engine setting.
- Change the list of words to filter from search
- Change cache settings

Usage
-----

[](#usage)

### Configuration with config file

[](#configuration-with-config-file)

*Publish the config first.*

- Define the models that need to be used in search engine
- set default search conditions
- define the output variables.

#### Search engine configuration

[](#search-engine-configuration)

Base configuration for the search engine in the config-file

```
'settings' => [
    '[CONFIG-NAME]' => [                      // Engine configuration name (standard preset: 'default')
        'searchQuery' => '[PRESET SEARCH]',   // Optional: preset search words, results directly shown
        'pagination' => 20,                   // Optional: search items per page , default = 15
        'parameters' => [                     // Optional: specific names query strings parameters
            'search_query' => '[NAME]',       // search terms, default: q
            'actual_page' => '[NAME]',        // result page, default: p
            'actual_filter' => '[NAME]'       // result filter, default: f
        ],
        // Model configuration
        ...
    ]
]
```

##### Searchable model configuration

[](#searchable-model-configuration)

```
[
    '[MODEL\NAMESPACE]' => [
        'searchFields' => [
            [COLUMNAME] => [PRIORITY],
            ...
        ],
        'conditions' => [ // Set search conditions (optional)
            [COLUMNNAME] => [VALUE | METHOD | CLOSURE],
            ...
        ],
        'resultFields' => [
            [VARIABLENAME] => [COLUMNNAME | METHOD | CLOSURE],
            ...
        ]
    ]
]
```

##### Example

[](#example)

The example below shows the configuration of a search engine named 'global'.

```
'settings' => [
    'default' => [                      // Settings name 'default'
        'searchQuery' => 'linux',       // Preset search 'linux'
        'App\Models\Articles' => [      // Model to search: 'Articles'
            'searchFields' => [
                'title' => 2.5,         // Column: title,  priority: 2.5 (extra weight)
                'lead' => 2,            // Column: lead,   priority: 2
                'body' => 1,
                                        // In query: title LIKE '%[search words]%' OR lead LIKE '%[search words]%' ...
            ],
            'conditions' => [
                'active' => 1,          // query: active = 1 AND published = 1
                'published' => 1,       //
            ],
            'resultFields' => [
                'title' => 'pagetitle', // pagetitle usable as {{ $title }}
                'lead' => 'intotext',   // introtext => {{ $lead }}
                'url' => 'getSlug',     // call the method getSlug() in App\Models\Articles
                                        // Closure to format a date in search result
                'date' => fn () => \Carbon\Carbon::parse($this->created_at)->format('d/m/Y')
        ]
    ]
],
```

In short, one can defining multiple search engines. This makes it possible to create multiple specific search sets that behave different. You can create a global search engine or one for a specific page/model.

[See example of large configuration file with methods an closures](#example-config)

#### Rename query strings parameters

[](#rename-query-strings-parameters)

Renaming the GET variables that appear in the URL

```
// Default
search?q=some search words&p=1&f=articles

// Modified using the setting below
search?search=some search words&page=1&filter=articles
```

```
'settings' => [
    'default' => [                              // Config name 'default'
            'variables' => [
                'search_query' => 'search',     // search terms
                'actual_page' => 'page',        // result page
                'actual_filter' => 'filter'     // result filter
            ],
            'App\Models\Articles' => [
            ...
```

#### Preset search words

[](#preset-search-words)

It is possible to fire a searchQuery by default.

In config file

```
'settings' => [
    'default' => [
        'searchQuery' => 'Adobe',           // Set default search
        ...
```

#### Search Result items per page

[](#search-result-items-per-page)

Change search result items per page

```
'settings' => [
    'default' => [
        'pagination' => 30,                 // 30 items per page, default is 15
```

```
'settings' => [
    'default' => [
        'pagination' => false,                 // All results on one page
```

#### Example config

[](#example-config)

A setup for three different search engine configurations, each in a other situation with more specific search results

- Global website search
- Book / Chapter search
- Article searching

```
