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/ */
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);
}
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;
}
}