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

Commit f766d93

Browse files
author
maxiloc
committed
Merge pull request #18 from forkify/docs
Cleanup even more in the docs
2 parents 4b9df82 + 6d92cef commit f766d93

File tree

1 file changed

+63
-40
lines changed

1 file changed

+63
-40
lines changed

README.md

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Add `algolia/algoliasearch-laravel` to your `composer.json` file:
2626
composer require algolia/algoliasearch-laravel
2727
```
2828

29-
Add the service provider to ```config/app.php``` in the `providers` array.
29+
Add the service provider to `config/app.php` in the `providers` array.
3030

3131
```php
3232
AlgoliaSearch\Laravel\AlgoliaServiceProvider::class
@@ -44,7 +44,7 @@ This will create a `config/algolia.php` file in your app that you can modify to
4444

4545
## Quick Start
4646

47-
The following code will create a <code>Contact</code> add search capabilities to your <code>Contact</code> model:
47+
The following code will create a `Contact` add search capabilities to your `Contact` model:
4848

4949
```php
5050
use Illuminate\Database\Eloquent\Model;
@@ -68,11 +68,9 @@ class Contact extends Model
6868

6969
public function getAlgoliaRecord()
7070
{
71-
$extraData = [
71+
return array_merge($this->toArray(), [
7272
'custom_name' => 'Custom Name'
73-
];
74-
75-
return array_merge($this->toArray(), $extraData);
73+
]);
7674
}
7775
}
7876
```
@@ -89,8 +87,14 @@ class Contact extends Model
8987
use AlgoliaEloquentTrait;
9088

9189
public $algoliaSettings = [
92-
'attributesToIndex => ['id', 'name'],
93-
'customRanking => ['desc(popularity)', 'asc(name)'],
90+
'attributesToIndex' => [
91+
'id',
92+
'name',
93+
],
94+
'customRanking => [
95+
'desc(popularity)',
96+
'asc(name)',
97+
],
9498
];
9599
}
96100
```
@@ -119,10 +123,10 @@ index.search('something', function(success, hits) {
119123

120124
#### Backend Search
121125

122-
You could also use `search` but it's not recommended. This method will search on Algolia
126+
You could also use `search` but it's not recommended. This method will search on Algolia.
123127

124128
```php
125-
Contact::search("jon doe");
129+
Contact::search('jon doe');
126130
```
127131

128132
## Options
@@ -134,7 +138,9 @@ Each time a record is saved; it will be - asynchronously - indexed. On the other
134138
You can disable auto-indexing and auto-removing setting the following options:
135139

136140
```php
137-
class Contact extends \Illuminate\Database\Eloquent\Model
141+
use Illuminate\Database\Eloquent\Model;
142+
143+
class Contact extends Model
138144
{
139145
use AlgoliaEloquentTrait;
140146

@@ -158,10 +164,12 @@ Contact::reindex(); // Will use batch operations.
158164

159165
#### Custom Index Name
160166

161-
By default, the index name will be the class name pluriazed, e.g. "Contacts". You can customize the index name by using the `indices` option:
167+
By default, the index name will be the class name pluriazed, e.g. "Contacts". You can customize the index name by using the `$indices` option:
162168

163169
```php
164-
class Contact extends \Illuminate\Database\Eloquent\Model
170+
use Illuminate\Database\Eloquent\Model;
171+
172+
class Contact extends Model
165173
{
166174
use AlgoliaEloquentTrait;
167175

@@ -174,20 +182,24 @@ class Contact extends \Illuminate\Database\Eloquent\Model
174182
You can suffix the index name with the current Rails environment using the following option:
175183

176184
```php
177-
class Contact extends \Illuminate\Database\Eloquent\Model
185+
use Illuminate\Database\Eloquent\Model;
186+
187+
class Contact extends Model
178188
{
179189
use AlgoliaEloquentTrait;
180190

181-
public $perEnvironment = true; # index name will be "Contacts_{\App::environnement()}"
191+
public $perEnvironment = true; // Index name will be 'Contacts_{\App::environnement()}';
182192
}
183193
```
184194

185-
#### Custom ```objectID```
195+
#### Custom `objectID`
186196

187197
By default, the `objectID` is based on your record's keyName (`id` by default). You can change this behavior specifying the `object_id_key` option (be sure to use a uniq field).
188198

189199
```php
190-
class Contact extends \Illuminate\Database\Eloquent\Model
200+
use Illuminate\Database\Eloquent\Model;
201+
202+
class Contact extends Model
191203
{
192204
use AlgoliaEloquentTrait;
193205

@@ -197,20 +209,18 @@ class Contact extends \Illuminate\Database\Eloquent\Model
197209

198210
#### Restrict Indexing to a Subset of Your Data
199211

200-
You can add constraints controlling if a record must be indexed by defining indexOnly function.
212+
You can add constraints controlling if a record must be indexed by defining `indexOnly()` method.
201213

202214
```php
203-
class Contact extends \Illuminate\Database\Eloquent\Model
215+
use Illuminate\Database\Eloquent\Model;
216+
217+
class Contact extends Model
204218
{
205219
use AlgoliaEloquentTrait;
206220

207221
public function indexOnly($index_name)
208222
{
209-
if ($condition) {
210-
return 1;
211-
}
212-
213-
return 0;
223+
return (bool) $condition;
214224
}
215225
}
216226
```
@@ -219,56 +229,66 @@ class Contact extends \Illuminate\Database\Eloquent\Model
219229

220230
#### Manual Indexing
221231

222-
You can trigger indexing using the <code>pushToindex</code> instance method.
232+
You can trigger indexing using the `pushToindex()` instance method.
223233

224234
```php
225-
$c = Contact::firstOrCreate(['name' => 'Jean']);
226-
$c->pushToindex();
235+
$contact = Contact::firstOrCreate(['name' => 'Jean']);
236+
$contact->pushToindex();
227237
```
228238

229239
#### Manual Removal
230240

231-
And trigger index removing using the <code>removeFromIndex</code> instance method.
241+
And trigger index removing using the `removeFromIndex()` instance method.
232242

233243
```php
234-
$c = Contact::firstOrCreate(['name' => 'Jean']);
235-
$c->removeFromindex();
244+
$contact = Contact::firstOrCreate(['name' => 'Jean']);
245+
$contact->removeFromindex();
236246
```
237247
#### Reindexing
238248

239-
To *safely* reindex all your records (index to a temporary index + move the temporary index to the current one atomically), use the <code>reindex</code> class method:
249+
To *safely* reindex all your records (index to a temporary index + move the temporary index to the current one atomically), use the `reindex` class method:
240250

241251
```php
242-
Contact::reindex()
252+
Contact::reindex();
243253
```
244254

245255
To reindex all your records (in place, without deleting out-dated records):
246256

247257
```php
248-
Contact::reindex(false)
258+
Contact::reindex(false);
249259
```
250260

251261
#### Clearing an Index
252262

253-
To clear an index, use the <code>clear_index!</code> class method:
263+
To clear an index, use the `clear_index!` class method:
254264

255265
```ruby
256-
Contact::clearIndices()
266+
Contact::clearIndices();
257267
```
258268

259269
## Master/Slave
260270

261271
You can define slave indexes in the `$algolia_settings` variable:
262272

263273
```php
264-
class Contact extends \Illuminate\Database\Eloquent\Model
274+
use Illuminate\Database\Eloquent\Model;
275+
276+
class Contact extends Model
265277
{
266278
use AlgoliaEloquentTrait;
267279

268280
public $algoliaSettings = [
269-
'attributesToIndex' => ['id', 'name'],
270-
'customRanking' => ['desc(popularity)', 'asc(name)'],
271-
'slaves' => ['contacts_desc']
281+
'attributesToIndex' => [
282+
'id',
283+
'name',
284+
],
285+
'customRanking' => [
286+
'desc(popularity)',
287+
'asc(name)',
288+
],
289+
'slaves' => [
290+
'contacts_desc',
291+
],
272292
];
273293

274294
public $slaves_settings = [
@@ -305,7 +325,10 @@ class Contact extends Model
305325
{
306326
use AlgoliaEloquentTrait;
307327

308-
public $indices = ['contact_public', 'contact_private'];
328+
public $indices = [
329+
'contact_public',
330+
'contact_private',
331+
];
309332

310333
public function indexOnly($indexName)
311334
{

0 commit comments

Comments
 (0)