Created
April 10, 2020 01:21
-
-
Save SalemCode8/5195db022d76af98bea6d94f4bffbb87 to your computer and use it in GitHub Desktop.
Add Search for Laravel ORM
This file contains hidden or 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\Traits; | |
| use Illuminate\Database\Eloquent\Builder; | |
| /** | |
| * Trait Searchable | |
| * @package App\Traits | |
| * @property array searchable | |
| */ | |
| trait HasSearch | |
| { | |
| /** | |
| * Scope a query to only include popular users. | |
| * | |
| * @param Builder $query | |
| * @param $term | |
| * @return Builder | |
| */ | |
| public function scopeSearch($query, $term): Builder | |
| { | |
| foreach ($this->searchable as $item){ | |
| $query->where($item, 'LIKE', '%'. $term . '%'); | |
| } | |
| return $query; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment