PHP code example of algolia / algoliasearch-laravel-4

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

    

algolia / algoliasearch-laravel-4 example snippets


AlgoliaSearch\Laravel\AlgoliaServiceProvider::class



return array(
    'id' => 'APP_ID',
    'key' => 'SECRET_KEY'
);

use Illuminate\Database\Eloquent\Model;

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)',
    	],
    ];
}

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use AlgoliaEloquentTrait;

    public $algoliaSettings = [
        'synonyms' => [
            [
                'objectID' => 'red-color',
                'type'     => 'synonym',
                'synonyms' => ['red', 'another red', 'yet another red']
            ]
        ]
    ];
}

Contact::setSettings();

Contact::search('jon doe');

Contact::search('jon doe', array('hitsPerPage' => 5));

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);

Contact::clearIndices();

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']);
algolia.php