@@ -116,9 +116,9 @@ abstract class Field implements Arrayable, JsonSerializable
116116 protected bool |Closure $ searchable = false ;
117117
118118 /**
119- * The search query resolver callback.
119+ * The filter query resolver callback.
120120 */
121- protected ?Closure $ searchQueryResolver = null ;
121+ protected ?Closure $ filterQueryResolver = null ;
122122
123123 /**
124124 * Determine if the field is computed.
@@ -313,35 +313,27 @@ public function isSortable(): bool
313313 /**
314314 * Set the searchable attribute.
315315 */
316- public function searchable (bool |Closure $ value = true ): static
316+ public function searchable (bool |Closure $ value = true , ? Closure $ callback = null ): static
317317 {
318- $ this ->searchable = $ value ;
318+ $ callback ??= function (Request $ request , Builder $ query , mixed $ value , string $ attribute ): Builder {
319+ return $ query ->where ($ query ->qualifyColumn ($ attribute ), 'like ' , "% {$ value }% " , 'or ' );
320+ };
319321
320- return $ this ;
322+ return $ this -> filterable ( $ callback , $ value ) ;
321323 }
322324
323325 /**
324326 * Determine if the field is searchable.
325327 */
326328 public function isSearchable (): bool
327329 {
328- if ($ this ->computed ) {
330+ if (! $ this ->isFilterable () ) {
329331 return false ;
330332 }
331333
332334 return $ this ->searchable instanceof Closure ? call_user_func ($ this ->searchable ) : $ this ->searchable ;
333335 }
334336
335- /**
336- * Set the search query resolver.
337- */
338- public function searchWithQuery (Closure $ callback ): static
339- {
340- $ this ->searchQueryResolver = $ callback ;
341-
342- return $ this ;
343- }
344-
345337 /**
346338 * Set the translatable attribute.
347339 */
@@ -365,13 +357,35 @@ public function isTranslatable(): bool
365357 }
366358
367359 /**
368- * Resolve the search query.
360+ * Set the filterable attribute.
361+ */
362+ public function filterable (?Closure $ callback = null , bool |Closure $ search = false ): static
363+ {
364+ $ this ->searchable = $ search ;
365+
366+ $ this ->filterQueryResolver = $ callback ?: function (Request $ request , Builder $ query , mixed $ value , string $ attribute ): Builder {
367+ return $ query ->where ($ query ->qualifyColumn ($ attribute ), $ value );
368+ };
369+
370+ return $ this ;
371+ }
372+
373+ /**
374+ * Determine whether the field is filterable.
375+ */
376+ public function isFilterable (): bool
377+ {
378+ return ! $ this ->computed && $ this ->filterQueryResolver instanceof Closure;
379+ }
380+
381+ /**
382+ * Resolve the filter query.
369383 */
370- public function resolveSearchQuery (Request $ request , Builder $ query , mixed $ value ): Builder
384+ public function resolveFilterQuery (Request $ request , Builder $ query , mixed $ value ): Builder
371385 {
372- return is_null ( $ this ->searchQueryResolver )
373- ? $ query
374- : call_user_func_array ( $ this -> searchQueryResolver , [ $ request , $ query, $ value ]) ;
386+ return $ this ->isFilterable ( )
387+ ? call_user_func_array ( $ this -> filterQueryResolver , [ $ request , $ query, $ value , $ this -> getModelAttribute ()])
388+ : $ query ;
375389 }
376390
377391 /**
0 commit comments