Skip to content

Instantly share code, notes, and snippets.

@SalemCode8
Created April 10, 2020 01:21
Show Gist options
  • Select an option

  • Save SalemCode8/5195db022d76af98bea6d94f4bffbb87 to your computer and use it in GitHub Desktop.

Select an option

Save SalemCode8/5195db022d76af98bea6d94f4bffbb87 to your computer and use it in GitHub Desktop.
Add Search for Laravel ORM
<?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