Skip to content

Commit 5b93959

Browse files
Merge pull request #11 from algolia/feat/guarded
Let hydrate() take `$guarded` into account
2 parents 1e1f32a + d4ccbcd commit 5b93959

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,25 @@ to avoid populating stale data.
4848

4949
#### Restrict attributes
5050

51-
By default, this method will add all attributes from Algolia's record to your model.
51+
By default, this method will add all attributes from Algolia's record to your model. If you want to remove sensitive or irrelevant data from your model, you have two options.
5252

53-
If you want to remove sensitive or irrelevant data from your model, you can set a list of retrievable attributes in your Algolia dashboard. In this case, Algolia will only return these attributes while still searching every `searchableAttributes`.
53+
You can set a list of retrievable attributes in your Algolia dashboard. In this case, Algolia will only return these attributes while still searching every `searchableAttributes`.
54+
55+
You may as well use the laravel `$guarded` attributes of your model class. For instance, if you don't want to see the `_h` attribute in your collection, you will have the following.
56+
57+
```php
58+
namespace App;
59+
60+
use Illuminate\Database\Eloquent\Model;
61+
use Laravel\Scout\Searchable;
62+
63+
class People extends Model
64+
{
65+
use Searchable;
66+
67+
protected $guarded = ['_highlightResult'];
68+
}
69+
```
5470

5571
### `with`
5672

src/macros.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@
102102
$className = get_class($this->model);
103103
$models = new Collection();
104104

105-
Eloquent::unguard();
105+
/* If the model is fully guarded, we unguard it.
106+
Fully garded is the default configuration and it will
107+
only result in error.
108+
If the `$guarded` attribute is set to a list of attribute
109+
we take it into account. */
110+
if (in_array('*', $this->model->getGuarded())) {
111+
Eloquent::unguard();
112+
}
106113

107114
$hits->each(function($item, $key) use ($className, $models) {
108115
$models->push(new $className($item));

0 commit comments

Comments
 (0)