PHP code example of thaoha / eloquent-search

1. Go to this page and download the library: Download thaoha/eloquent-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thaoha / eloquent-search example snippets


'providers' => [
    ...
    EloquentEs\EloquentEsServiceProvider::class,
],

$app->register(EloquentEs\EloquentEsServiceProvider::class);

use EloquentEs\Supports\ElasticModelTrait;

/**
 * Class Company
 * @package App\Models
 */
class Company extends Model
{
    use ElasticModelTrait;
    ...
}

$app->configure('elastic');

App\Models\Company::esCreateIndex();

/**
     * Index mapping
     *
     * @var array
     */
    private $esIndexMapping = [
        'id'            => ['type' => 'long'],
        'name'          => ['type' => 'string'],
        'company'       => ['type' => 'string']
    ];

App\Models\Company::esPutMapping();

App\Models\Company::esDeleteIndex();

App\Models\Company::esReset();

$company = App\Models\Company::find(1);
$company->esIndex();

/**
     * Get eloquent model data
     *
     * @return array
     */
    public function esSerialize()
    {
        $data = $this->toArray();
        $data['user_id'] = 1;
        
        return $data;
    }

$company = App\Models\Company::find(1);
$company->esDelete();

$company = App\Models\Company::find(1);
$company->esReindex();

$params = [
    'match' => ['name' => 'keyword']
];
$hits = App\Models\Company::esSearch($params);
shell
$ php artisan vendor:publish --provider="EloquentEs\EloquentEsServiceProvider"