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
Copy file name to clipboardExpand all lines: README.md
+83-30Lines changed: 83 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ Hey! We've built a Docker-based deployment tool to launch apps and sites fully c
23
23
* Support for cross-model [pagination](https://laravel.com/docs/master/pagination#introduction).
24
24
* Search through single or multiple columns.
25
25
* Search through (nested) relationships.
26
+
* Support for Full-Text Search, even through relationships.
26
27
* Order by (cross-model) columns or by relevance.
27
28
* Use [constraints](https://laravel.com/docs/master/eloquent#retrieving-models) and [scoped queries](https://laravel.com/docs/master/eloquent#query-scopes).
28
29
*[Eager load relationships](https://laravel.com/docs/master/eloquent-relationships#eager-loading) for each model.
@@ -47,24 +48,30 @@ You can install the package via composer:
* The `addWhen` method has been removed in favor of [`when`](#usage).
55
+
* By default, the results are sorted by the *updated* column, which is the `updated_at` column in most cases. If you don't use timestamps, it will now use the primary key by default.
56
+
57
+
## Upgrading from v1 to v2
51
58
52
59
* The `startWithWildcard` method has been renamed to `beginWithWildcard`.
53
60
* 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.
54
61
* The `allowEmptySearchQuery` method and `EmptySearchQueryException` class have been removed, but you can still [get results without searching](#getting-results-without-searching).
55
62
56
63
## Usage
57
64
58
-
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.
65
+
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 `search` method with the search term, and you'll get a `\Illuminate\Database\Eloquent\Collection` instance with the results.
59
66
60
-
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.
67
+
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. If you don't use timestamps at all, it will use the primary key by default. Of course, you can [order by another column](#sorting) as well.
61
68
62
69
```php
63
70
use ProtoneMedia\LaravelCrossEloquentSearch\Search;
64
71
65
72
$results = Search::add(Post::class, 'title')
66
73
->add(Video::class, 'title')
67
-
->get('howto');
74
+
->search('howto');
68
75
```
69
76
70
77
If you care about indentation, you can optionally use the `new` method on the facade:
@@ -73,7 +80,7 @@ If you care about indentation, you can optionally use the `new` method on the fa
73
80
Search::new()
74
81
->add(Post::class, 'title')
75
82
->add(Video::class, 'title')
76
-
->get('howto');
83
+
->search('howto');
77
84
```
78
85
79
86
You can add multiple models at once by using the `addMany` method:
@@ -82,16 +89,16 @@ You can add multiple models at once by using the `addMany` method:
82
89
Search::addMany([
83
90
[Post::class, 'title'],
84
91
[Video::class, 'title'],
85
-
])->get('howto');
92
+
])->search('howto');
86
93
```
87
94
88
-
There's also an `addWhen` method, that adds the model when the first argument given to the method evaluates to `true`:
95
+
There's also an `when` method to apply certain clauses based on another condition:
@@ -124,7 +131,7 @@ Multi-word search is supported out of the box. Simply wrap your phrase into doub
124
131
```php
125
132
Search::add(Post::class, 'title')
126
133
->add(Video::class, 'title')
127
-
->get('"macos big sur"');
134
+
->search('"macos big sur"');
128
135
```
129
136
130
137
You can disable the parsing of the search term by calling the `dontParseTerm` method, which gives you the same results as using double-quotes.
@@ -133,7 +140,7 @@ You can disable the parsing of the search term by calling the `dontParseTerm` me
133
140
Search::add(Post::class, 'title')
134
141
->add(Video::class, 'title')
135
142
->dontParseTerm()
136
-
->get('macos big sur');
143
+
->search('macos big sur');
137
144
```
138
145
139
146
### Sorting
@@ -144,7 +151,7 @@ If you want to sort the results by another column, you can pass that column to t
144
151
Search::add(Post::class, 'title', 'published_at')
145
152
->add(Video::class, 'title', 'released_at')
146
153
->orderByDesc()
147
-
->get('learn');
154
+
->search('learn');
148
155
```
149
156
150
157
You can call the `orderByRelevance` method to sort the results by the number of occurrences of the search terms. Imagine these two sentences:
@@ -158,7 +165,7 @@ If you search for *Apple iPad*, the second sentence will come up first, as there
158
165
Search::add(Post::class, 'title')
159
166
->beginWithWildcard()
160
167
->orderByRelevance()
161
-
->get('Apple iPad');
168
+
->search('Apple iPad');
162
169
```
163
170
164
171
Ordering by relevance is *not* supported if you're searching through (nested) relationships.
@@ -173,12 +180,12 @@ Search::new()
173
180
->orderByModel([
174
181
Post::class, Video::class, Comment::class,
175
182
])
176
-
->get('Artisan School');
183
+
->search('Artisan School');
177
184
```
178
185
179
186
### Pagination
180
187
181
-
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.
188
+
We highly recommend paginating your results. Call the `paginate` method before the `search` 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.
You may also use [simple pagination](https://laravel.com/docs/master/pagination#simple-pagination). This will return an instance of `\Illuminate\Contracts\Pagination\Paginator`, which is not length aware:
@@ -211,7 +218,7 @@ Instead of the class name, you can also pass an instance of the [Eloquent query
211
218
```php
212
219
Search::add(Post::published(), 'title')
213
220
->add(Video::where('views', '>', 2500), 'title')
214
-
->get('compile');
221
+
->search('compile');
215
222
```
216
223
217
224
### Multiple columns per model
@@ -221,7 +228,7 @@ You can search through multiple columns by passing an array of columns as the se
221
228
```php
222
229
Search::add(Post::class, ['title', 'body'])
223
230
->add(Video::class, ['title', 'subtitle'])
224
-
->get('eloquent');
231
+
->search('eloquent');
225
232
```
226
233
227
234
### Search through (nested) relationships
@@ -231,7 +238,30 @@ You can search through (nested) relationships by using the *dot* notation:
231
238
```php
232
239
Search::add(Post::class, ['comments.body'])
233
240
->add(Video::class, ['posts.user.biography'])
234
-
->get('solution');
241
+
->search('solution');
242
+
```
243
+
244
+
### Full-Text Search
245
+
246
+
You may use [MySQL's Full-Text Search](https://laravel.com/docs/master/queries#full-text-where-clauses) by using the `addFullText` method. You can search through a single or multiple columns (using [full text indexes](https://laravel.com/docs/master/migrations#available-index-types)), and you can specify a set of options, for example, to specify the mode. You can even regular and full-text searches in one query:
If you want to search through relationships, you need to pass in an array where the array key contains the relation, while the value is an array of columns:
257
+
258
+
```php
259
+
Search::new()
260
+
->addFullText(Page::class, [
261
+
'posts' => ['title', 'body'],
262
+
'sections' => ['title', 'subtitle', 'body'],
263
+
], )
264
+
->search('framework -css');
235
265
```
236
266
237
267
### Sounds like
@@ -243,7 +273,7 @@ Search::new()
243
273
->add(Post::class, 'framework')
244
274
->add(Video::class, 'framework')
245
275
->soundsLike()
246
-
->get('larafel');
276
+
->search('larafel');
247
277
```
248
278
249
279
### Eager load relationships
@@ -253,19 +283,19 @@ Not much to explain here, but this is supported as well :)
253
283
```php
254
284
Search::add(Post::with('comments'), 'title')
255
285
->add(Video::with('likes'), 'title')
256
-
->get('guitar');
286
+
->search('guitar');
257
287
```
258
288
259
289
### Getting results without searching
260
290
261
-
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):
291
+
You call the `search` 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):
0 commit comments