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

Commit 6b1595d

Browse files
committed
Document relashionship
1 parent ff0ee8d commit 6b1595d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,47 @@ class Contact extends Model
225225
}
226226
```
227227

228+
### Relashionships
229+
230+
By default the Algolia package will fetch the **loaded** relashionships.
231+
232+
If you 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.
233+
234+
It will look like:
235+
236+
```
237+
public function getAlgoliaRecord()
238+
{
239+
/**
240+
* Load the categories relation so that it's available
241+
* in the laravel toArray method
242+
*/
243+
$this->categories;
244+
245+
return $this->toArray();
246+
}
247+
```
248+
249+
In the resulted object you will have categories converted to array by Laravel. If you a custom relation structure you will instead do something like :
250+
251+
```
252+
public function getAlgoliaRecord()
253+
{
254+
/**
255+
* Load the categories relation so that it's available
256+
* in the laravel toArray method
257+
*/
258+
$extra_data = [];
259+
$extra_data['categories'] = array_map(function ($data) {
260+
return $data['name'];
261+
}, $this->categories->toArray();
262+
263+
return array_merge($this->toArray(), $extra_data);
264+
}
265+
```
266+
267+
268+
228269
## Indexing
229270

230271
#### Manual Indexing

0 commit comments

Comments
 (0)