Skip to content

Commit

Permalink
Update README to include a nice way to get Eloquent models from the S…
Browse files Browse the repository at this point in the history
…earchy results
  • Loading branch information
TomLingham committed May 14, 2016
1 parent 19d7db2 commit 8566a0a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ $users = Searchy::users('first_name::last_name')->select('first_name')->query('J

This will, however, also return the `relevance` aliased column regardless of what is entered here.

## How to get a Laravel Eloquent Collection

Transforming the search results into a collection of Laravel Eloquent models is outside the scope of this project. However, an easy way to achieve this without hitting your database more than necessary is to use the Eloquent `fill()` method.

```php
$users = collect(array_map(function($result) {
return (new \App\User())->fill(get_object_vars($result));
}, Searchy::users('name', 'email')->query('Andrew')->get()));
```

All this does it map a function over the Searchy results and then creates a new instance of the User model and hydrates the model using the `fill()` method.
Then once it has an array of all the Eloquent User models, it simply uses the Laravel `collect()` method to return the array as Laravel Collection which is the same as you would receive from querying the Laravel model directly. `get_object_vars()` is simply a PHP method to extract the accessible object variables as an associative array.

## Configuration

You can publish the configuration file to your `app` directory and override the settings by running `php artisan vendor:publish` to copy the configuration to your config folder as `searchy.php`
Expand Down

0 comments on commit 8566a0a

Please sign in to comment.