Skip to content
This repository was archived by the owner on Jul 31, 2018. It is now read-only.

Commit cbdef88

Browse files
author
maxiloc
authored
Update README.md
1 parent 083ceae commit cbdef88

File tree

1 file changed

+84
-67
lines changed

1 file changed

+84
-67
lines changed

README.md

Lines changed: 84 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ This PHP package integrates the Algolia Search API to the Laravel Eloquent ORM.
2727

2828
## Install
2929

30+
### Install via composer
3031
Add `algolia/algoliasearch-laravel` to your `composer.json` file:
3132

3233
```bash
3334
composer require algolia/algoliasearch-laravel
3435
```
3536

37+
### Service provider
3638
Add the service provider to `config/app.php` in the `providers` array.
3739

3840
```php
@@ -41,6 +43,8 @@ AlgoliaSearch\Laravel\AlgoliaServiceProvider::class
4143

4244
## Configuration
4345

46+
### Publish vendor
47+
4448
Laravel Algolia requires a connection configuration. To get started, you'll need to publish all vendor assets:
4549

4650
```bash
@@ -53,6 +57,8 @@ This will create a `config/algolia.php` file in your app that you can modify to
5357

5458
## Quick Start
5559

60+
### Quick Start
61+
5662
The following code adds search capabilities to your `Contact` model creating a `Contact` index:
5763

5864
```php
@@ -95,14 +101,14 @@ class Contact extends Model
95101
use AlgoliaEloquentTrait;
96102

97103
public $algoliaSettings = [
98-
'attributesToIndex' => [
99-
'id',
100-
'name',
101-
],
102-
'customRanking' => [
103-
'desc(popularity)',
104-
'asc(name)',
105-
],
104+
'attributesToIndex' => [
105+
'id',
106+
'name',
107+
],
108+
'customRanking' => [
109+
'desc(popularity)',
110+
'asc(name)',
111+
],
106112
];
107113
}
108114
```
@@ -183,10 +189,10 @@ use Illuminate\Database\Eloquent\Model;
183189

184190
class Contact extends Model
185191
{
186-
use AlgoliaEloquentTrait;
192+
use AlgoliaEloquentTrait;
187193

188-
public static $autoIndex = false;
189-
public static $autoDelete = false;
194+
public static $autoIndex = false;
195+
public static $autoDelete = false;
190196
}
191197
```
192198

@@ -197,7 +203,7 @@ Contact::$autoIndex = false;
197203
Contact::clearIndices();
198204

199205
for ($i = 0; $i < 10000; $i++) {
200-
$contact = Contact::firstOrCreate(['name' => 'Jean']);
206+
$contact = Contact::firstOrCreate(['name' => 'Jean']);
201207
}
202208

203209
Contact::reindex(); // Will use batch operations.
@@ -211,25 +217,25 @@ use Illuminate\Database\Eloquent\Model;
211217

212218
class Contact extends Model
213219
{
214-
use AlgoliaEloquentTrait;
220+
use AlgoliaEloquentTrait;
215221

216-
public function autoIndex()
217-
{
218-
if (\App::environment() === 'test') {
219-
return false;
220-
}
222+
public function autoIndex()
223+
{
224+
if (\App::environment() === 'test') {
225+
return false;
226+
}
221227

222-
return true;
223-
}
228+
return true;
229+
}
224230

225-
public static autoDelete()
226-
{
231+
public static autoDelete()
232+
{
227233
if (\App::environment() === 'test') {
228234
return false;
229235
}
230236

231237
return true;
232-
}
238+
}
233239
}
234240
```
235241

@@ -278,7 +284,7 @@ class Contact extends Model
278284
{
279285
use AlgoliaEloquentTrait;
280286

281-
public static $objectIdKey = 'new_key';
287+
public static $objectIdKey = 'new_key';
282288
}
283289
```
284290

@@ -291,17 +297,19 @@ use Illuminate\Database\Eloquent\Model;
291297

292298
class Contact extends Model
293299
{
294-
use AlgoliaEloquentTrait;
300+
use AlgoliaEloquentTrait;
295301

296-
public function indexOnly($index_name)
297-
{
298-
return (bool) $condition;
299-
}
302+
public function indexOnly($index_name)
303+
{
304+
return (bool) $condition;
305+
}
300306
}
301307
```
302308

303309
## Relationships
304310

311+
### Relationships
312+
305313
By default the Algolia package will fetch the **loaded** relationships.
306314

307315
If you want to index records that didn't yet load any relations you can do it by loading them in the ```getAlgoliaRecord``` that you can create in your model.
@@ -311,11 +319,11 @@ It will look like:
311319
```php
312320
public function getAlgoliaRecord()
313321
{
314-
/**
315-
* Load the categories relation so that it's available
316-
* in the laravel toArray method
317-
*/
318-
$this->categories;
322+
/**
323+
* Load the categories relation so that it's available
324+
* in the laravel toArray method
325+
*/
326+
$this->categories;
319327

320328
return $this->toArray();
321329
}
@@ -326,14 +334,14 @@ In the resulted object you will have categories converted to array by Laravel. I
326334
```php
327335
public function getAlgoliaRecord()
328336
{
329-
/**
330-
* Load the categories relation so that it's available
331-
* in the laravel toArray method
332-
*/
333-
$extra_data = [];
334-
$extra_data['categories'] = array_map(function ($data) {
335-
return $data['name'];
336-
}, $this->categories->toArray();
337+
/**
338+
* Load the categories relation so that it's available
339+
* in the laravel toArray method
340+
*/
341+
$extra_data = [];
342+
$extra_data['categories'] = array_map(function ($data) {
343+
return $data['name'];
344+
}, $this->categories->toArray();
337345

338346
return array_merge($this->toArray(), $extra_data);
339347
}
@@ -385,27 +393,29 @@ Contact::clearIndices();
385393

386394
## Master/Slave
387395

396+
### Master/Slave
397+
388398
You can define slave indexes using the `$algolia_settings` variable:
389399

390400
```php
391401
use Illuminate\Database\Eloquent\Model;
392402

393403
class Contact extends Model
394404
{
395-
use AlgoliaEloquentTrait;
396-
397-
public $algoliaSettings = [
398-
'attributesToIndex' => [
399-
'id',
400-
'name',
401-
],
402-
'customRanking' => [
403-
'desc(popularity)',
404-
'asc(name)',
405-
],
406-
'slaves' => [
407-
'contacts_desc',
408-
],
405+
use AlgoliaEloquentTrait;
406+
407+
public $algoliaSettings = [
408+
'attributesToIndex' => [
409+
'id',
410+
'name',
411+
],
412+
'customRanking' => [
413+
'desc(popularity)',
414+
'asc(name)',
415+
],
416+
'slaves' => [
417+
'contacts_desc',
418+
],
409419
];
410420

411421
public $slavesSettings = [
@@ -433,27 +443,29 @@ Book::search('foo bar', ['index' => 'contacts_desc']);
433443

434444
## Target Multiple Indexes
435445

446+
### Target Multiple Indexes
447+
436448
You can index a record in several indexes using the <code>$indices</code> property:
437449

438450
```php
439451
use Illuminate\Database\Eloquent\Model;
440452

441453
class Contact extends Model
442454
{
443-
use AlgoliaEloquentTrait;
455+
use AlgoliaEloquentTrait;
444456

445-
public $indices = [
446-
'contact_public',
447-
'contact_private',
448-
];
457+
public $indices = [
458+
'contact_public',
459+
'contact_private',
460+
];
449461

450-
public function indexOnly($indexName)
451-
{
452-
if ($indexName == 'contact_public')
453-
return true;
462+
public function indexOnly($indexName)
463+
{
464+
if ($indexName == 'contact_public')
465+
return true;
454466

455-
return $this->private;
456-
}
467+
return $this->private;
468+
}
457469

458470
}
459471
```
@@ -466,6 +478,8 @@ Book::search('foo bar', ['index' => 'contacts_private']);
466478

467479
## Eloquent compatibility
468480

481+
### Eloquent compatibility
482+
469483
Doing :
470484

471485
```php
@@ -484,8 +498,11 @@ Ad::find($id)->update($attributes);
484498
<!--NO_HTML-->
485499
## Compatibility
486500

501+
### Compatibility
502+
487503
Compatible with 5.x applications
488504

505+
## License
489506
### License
490507

491508
Laravel Algolia Search is licensed under [The MIT License (MIT)](LICENSE).

0 commit comments

Comments
 (0)