Created
April 10, 2017 14:40
-
-
Save tomfordweb/614d4640cd71a8c12936b8360b529532 to your computer and use it in GitHub Desktop.
L5 Searchable Trait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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