You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://plant.treeware.earth/protonemedia/laravel-cross-eloquent-search)
8
8
9
-
This Laravel package allows you to search through multiple Eloquent models. It supports sorting, pagination, scoped queries, eager load relationships and searching through single or multiple columns.
9
+
This Laravel package allows you to search through multiple Eloquent models. It supports sorting, pagination, scoped queries, eager load relationships, and searching through single or multiple columns.
10
10
11
11
### 📺 Want to watch an implementation of this package? Rewatch the live stream (skip to 13:44 for the good stuff): [https://youtu.be/WigAaQsPgSA](https://youtu.be/WigAaQsPgSA)
12
12
@@ -26,9 +26,9 @@ This Laravel package allows you to search through multiple Eloquent models. It s
26
26
* In-database [sorting](https://laravel.com/docs/master/queries#ordering-grouping-limit-and-offset) of the combined result.
27
27
* Zero third-party dependencies
28
28
29
-
## Blogpost
29
+
## Blog post
30
30
31
-
If you want to know more about the background of this package, please read [the blogpost](https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package).
31
+
If you want to know more about this package's background, please read [the blog post](https://protone.media/blog/search-through-multiple-eloquent-models-with-our-latest-laravel-package).
32
32
33
33
## Support
34
34
@@ -42,9 +42,17 @@ You can install the package via composer:
* The `startWithWildcard` method has been renamed to `beginWithWildcard`.
48
+
* The default order column is now evaluated by the `getUpdatedAtColumn` method. Previously it was hard-coded to `updated_at`. You still can use [another column](#sorting) to order by.
49
+
* The `allowEmptySearchQuery` method and `EmptySearchQueryException` class have been removed, but you can still [get results without searching](#getting-results-without-searching).
50
+
45
51
## Usage
46
52
47
-
Start your search query by adding one or more models to search through. Call the `add` method with the class name of the model and the column you want to search through. Then call the `get` method with the search term, and you'll get a `\Illuminate\Database\Eloquent\Collection` instance with the results. By default, the results are sorted in ascending order by the `updated_at` column.
53
+
Start your search query by adding one or more models to search through. Call the `add` method with the model's class name and the column you want to search through. Then call the `get` method with the search term, and you'll get a `\Illuminate\Database\Eloquent\Collection` instance with the results.
54
+
55
+
The results are sorted in ascending order by the *updated* column by default. In most cases, this column is `updated_at`. If you've [customized](https://laravel.com/docs/master/eloquent#timestamps) your model's `UPDATED_AT` constant, or overwritten the `getUpdatedAtColumn` method, this package will use the customized column. Of course, you can [order by another column](#sorting) as well.
48
56
49
57
```php
50
58
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
@@ -81,25 +89,26 @@ Search::new()
81
89
->get('howto');
82
90
```
83
91
84
-
### Sorting
92
+
### Wildcards
85
93
86
-
If you want to sort the results by another column, you can pass that column to the `add` method as a third parameter. Call the `orderByDesc` method to sort the results in descending order.
94
+
By default, we split up the search term, and each keyword will get a wildcard symbol to do partial matching. Practically this means the search term `apple ios` will result in `apple%` and `ios%`. If you want a wildcard symbol to begin with as well, you can call the `beginWithWildcard` method. This will result in `%apple%` and `%ios%`.
87
95
88
96
```php
89
-
Search::add(Post::class, 'title', 'publihed_at')
90
-
->add(Video::class, 'title', 'released_at')
91
-
->orderByDesc()
92
-
->get('learn');
97
+
Search::add(Post::class, 'title')
98
+
->add(Video::class, 'title')
99
+
->beginWithWildcard()
100
+
->get('os');
93
101
```
94
102
95
-
### Start search term with wildcard
103
+
*Note: in previous versions of this package, this method was called `startWithWildcard()`.*
96
104
97
-
By default, we split up the search term, and each keyword will get a wildcard symbol to do partial matching. Practically this means the search term `apple ios` will result in `apple%` and `ios%`. If you want a wildcard symbol to start with as well, you can call the `startWithWildcard` method. This will result in `%apple%` and `%ios%`.
105
+
If you want to disable the behaviour where a wildcard is appended to the terms, you should call the `endWithWildcard` method with `false`:
If you want to sort the results by another column, you can pass that column to the `add` method as a third parameter. Call the `orderByDesc` method to sort the results in descending order.
137
+
138
+
```php
139
+
Search::add(Post::class, 'title', 'publihed_at')
140
+
->add(Video::class, 'title', 'released_at')
141
+
->orderByDesc()
142
+
->get('learn');
143
+
```
144
+
125
145
### Pagination
126
146
127
-
We highly recommend to paginate your results. Call the `paginate` method before the `get` method, and you'll get an instance of `\Illuminate\Contracts\Pagination\LengthAwarePaginator` as a result. The `paginate` method takes three (optional) parameters to customize the paginator. These arguments are [the same](https://laravel.com/docs/master/pagination#introduction) as Laravel's database paginator.
147
+
We highly recommend paginating your results. Call the `paginate` method before the `get` method, and you'll get an instance of `\Illuminate\Contracts\Pagination\LengthAwarePaginator` as a result. The `paginate` method takes three (optional) parameters to customize the paginator. These arguments are [the same](https://laravel.com/docs/master/pagination#introduction) as Laravel's database paginator.
MySQL has a *soundex* algorithm built-in so you can search for terms that sound almost the same. You can use this feature by calling the `soundsLike` method:
If you call the `get` method without a term or with an empty term, the package throws an `EmptySearchQueryException`. You can disable this behaviour with the `allowEmptySearchQuery` method.
In this case, you can discard the second argument as well. With the `orderBy` method, you can set the column to sort by (previously the third argument):
217
+
You call the `get` method without a term or with an empty term. In this case, you can discard the second argument of the `add` method. With the `orderBy` method, you can set the column to sort by (previously the third argument):
196
218
197
219
```php
198
220
Search::add(Post::class)
199
221
->orderBy('published_at')
200
222
->add(Video::class)
201
223
->orderBy('released_at')
202
-
->allowEmptySearchQuery()
203
224
->get();
204
225
```
205
226
@@ -258,7 +279,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
258
279
259
280
### Security
260
281
261
-
If you discover any securityrelated issues, please email pascal@protone.media instead of using the issue tracker.
282
+
If you discover any security-related issues, please email pascal@protone.media instead of using the issue tracker.
262
283
263
284
## Credits
264
285
@@ -271,4 +292,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
271
292
272
293
## Treeware
273
294
274
-
This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/pascalbaljetmedia/laravel-cross-eloquent-search) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
295
+
This package is [Treeware](https://treeware.earth). If you use it in production, we ask that you [**buy the world a tree**](https://plant.treeware.earth/pascalbaljetmedia/laravel-cross-eloquent-search) to thank us for our work. By contributing to the Treeware forest, you'll create employment for local families and restoring wildlife habitats.
0 commit comments