PHP code example of japseyz / algoliasearch-laravel

1. Go to this page and download the library: Download japseyz/algoliasearch-laravel 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/ */

    

japseyz / algoliasearch-laravel example snippets


AlgoliaSearch\Laravel\AlgoliaServiceProvider::class

use Illuminate\Database\Eloquent\Model;
use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
    
    public function getAlgoliaRecord()
    {
        return array_merge($this->toArray(), [
            'custom_name' => 'Custom Name'
        ]);
    }
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
    
    public $algoliaSettings = [
    	'attributesToIndex' => [
    		'id', 
    		'name',
    	],
    	'customRanking' => [
    		'desc(popularity)', 
    		'asc(name)',
    	],
    ];
}

Contact::setSettings();

Contact::search('jon doe');

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
	use AlgoliaEloquentTrait;
    
	public static $autoIndex = false;
	public static $autoDelete = false;
}

Contact::$autoIndex = false;
Contact::clearIndices();

for ($i = 0; $i < 10000; $i++) {
	$contact = Contact::firstOrCreate(['name' => 'Jean']);
}

Contact::reindex(); // Will use batch operations.

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
    
    public $indices = ['contact_all'];
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
    
    public static $perEnvironment = true; // Index name will be 'Contacts_{\App::environnement()}';
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;
    
	public static $objectIdKey = 'new_key';
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
   	use AlgoliaEloquentTrait;
    
	public function indexOnly($index_name)
	{
		return (bool) $condition;
	}
}

public function getAlgoliaRecord()
{
	/**
	 * Load the categories relation so that it's available
	 * 	in the laravel toArray method
	 */
	$this->categories; 
  
   return $this->toArray();
}

public function getAlgoliaRecord()
{
	/**
	 * Load the categories relation so that it's available
	 * 	in the laravel toArray method
	 */
	$extra_data = [];
	$extra_data['categories'] = array_map(function ($data) {
							            return $data['name'];
						        }, $this->categories->toArray();
  
   return array_merge($this->toArray(), $extra_data);
}

$contact = Contact::firstOrCreate(['name' => 'Jean']);
$contact->pushToIndex();

$contact = Contact::firstOrCreate(['name' => 'Jean']);
$contact->removeFromIndex();

Contact::reindex();

Contact::reindex(false);

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
	 use AlgoliaEloquentTrait;
    
	 public $algoliaSettings = [
		'attributesToIndex' => [
			'id', 
			'name',
		],
    	'customRanking' => [
    		'desc(popularity)', 
    		'asc(name)',
    	],
    	'slaves' => [
    		'contacts_desc',
    	],
    ];

    public $slavesSettings = [
        'contacts_desc' => [
            'ranking' => [
                'desc(name)',
                'typo',
                'geo',
                'words',
                'proximity',
                'attribute',
                'exact',
                'custom'
            ]
        ]
    ];
}

Book::search('foo bar', ['index' => 'contacts_desc']);

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
	use AlgoliaEloquentTrait;
    
	public $indices = [
		'contact_public', 
		'contact_private',
	];
    
	public function indexOnly($indexName)
	{
		if ($indexName == 'contact_public')
			return true;

		return $this->private;
	}

}

Book::search('foo bar', ['index' => 'contacts_private']);
bash
php artisan vendor:publish