Skip to content

Commit ad9c6b3

Browse files
Merge pull request #10 from robbydooo/master
Adding hydrate macro & added documentation
2 parents 710667f + 0906e5b commit ad9c6b3

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,94 @@ Model::search('query')
5454
```
5555

5656

57+
## `with`
58+
59+
The `with` method gives you complete access to the Algolia options parameter. This allows you
60+
to customise the search parameters exactly the same as you would using the algolia php library directly.
61+
62+
```php
63+
64+
$filters = [
65+
'averge_ratings >= 3',
66+
'total_reviews >= 1'
67+
];
68+
69+
$filterString = implode(' AND ', $filters);
70+
71+
$params = [
72+
'aroundLatLng' => $lat.','.$lon,
73+
'hitsPerPage' => 30,
74+
'page' => 0,
75+
'aroundRadius' => 30000, //30km
76+
'aroundPrecision' => 200), //200 Meters
77+
'getRankingInfo' => true, //return ranking information in results
78+
'filters' => $filterString // add filters
79+
];
80+
81+
$result = Model::search('')->with($params)->get();
82+
83+
```
84+
85+
86+
## `hydrate`
87+
88+
The `hydrate` method is similar to the standard get() method, except it hydrates the models from your Algolia index
89+
instead of of using the objects keys to pull the related models from your database, meaning much quicker response times.
90+
91+
This also gives you the ability to overide the fill() method on any model to use the additional data that you store
92+
in your indexes.
93+
94+
Note: By using this method you must be sure that you are correctly keeping your algolia index in sync with your database
95+
to avoid populating stale data.
96+
97+
Example model with additional data from Algolia Index being populated:
98+
99+
```php
100+
101+
class ExampleModel extends Model
102+
{
103+
use Searchable;
104+
105+
protected $appends = [
106+
'rankingInfo' //Add rankingInfo when converted to array
107+
];
108+
109+
protected $rankingInfo = [];
110+
protected $highlightResult = [];
111+
112+
/**
113+
* Adds the ranking & highlight results from the search request to get search score/geo distance etc
114+
*
115+
* @param array $attributes
116+
* @return mixed
117+
*/
118+
public function fill(array $attributes)
119+
{
120+
121+
if (isset($attributes['_rankingInfo']))
122+
{
123+
$this->setRankingInfo($attributes['_rankingInfo']);
124+
}
125+
126+
//Add any additional data stored in algolia as required
127+
128+
return parent::fill($attributes);
129+
}
130+
131+
public function getRankingInfoAttribute(): array
132+
{
133+
return $this->rankingInfo;
134+
}
135+
136+
public function setRankingInfo(array $rankingInfo)
137+
{
138+
$this->rankingInfo = $rankingInfo;
139+
}
140+
141+
$result = ExampleModel::search('')->with($params)->get();
142+
143+
```
144+
57145
## Contributing
58146

59147
Feel free to open an issue to request a macro.

src/macros.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Laravel\Scout\Builder;
4+
use Illuminate\Database\Eloquent\Collection;
45

56

67
if (! Builder::hasMacro('count')) {
@@ -81,3 +82,34 @@
8182
return $this;
8283
});
8384
}
85+
86+
if (! Builder::hasMacro('hydrate')) {
87+
/**
88+
* get() hydrates records by looking up the Ids in the corresponding database
89+
* This macro uses the data returned from the search results to hydrate
90+
* the models and return a collection
91+
*
92+
* @return Collection
93+
*/
94+
Builder::macro('hydrate', function () {
95+
$results = $this->engine()->search($this);
96+
97+
if (count($results['hits']) === 0) {
98+
return Collection::make();
99+
}
100+
101+
$hits = collect($results['hits']);
102+
$className = get_class($this->model);
103+
$models = new Collection();
104+
105+
Eloquent::unguard();
106+
107+
$hits->each(function($item, $key) use ($className, $models) {
108+
$models->push(new $className($item));
109+
});
110+
111+
Eloquent::reguard();
112+
113+
return $models;
114+
});
115+
}

0 commit comments

Comments
 (0)