Skip to content

Instantly share code, notes, and snippets.

@tomfordweb
Created April 10, 2017 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfordweb/614d4640cd71a8c12936b8360b529532 to your computer and use it in GitHub Desktop.
Save tomfordweb/614d4640cd71a8c12936b8360b529532 to your computer and use it in GitHub Desktop.
L5 Searchable Trait
<?php namespace App;
trait Searchable {
/**
* Search for a model based off of the "fillable" property of the model
* @param type $query The query, no need to insert this into method as L typehints it
* @param string $term The search term
* @return type The modified query
*/
public function scopeSearchFor($query, $term = null)
{
if(is_null($term) || empty($term))
return $query;
$fields = $this->fillable;
$first = $fields[0]; #get the first var of the models searchable fields
$term = trim($term);
$query = $query->withTrashed();
if(count($fields) > 1) {
$query = $query->where($first, 'like', '%'.$term.'%');
unset($fields[0]); #unset it so we can start the filtering, and then iterate the columns
foreach($fields as $field)
$query = $query->orWhere($field, 'like', '%'.$term.'%');
} else {
$query = $query->where($first, 'like', '%'.$term.'%');
}
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment