Skip to content

Instantly share code, notes, and snippets.

@mul14
Last active February 10, 2023 00:55
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save mul14/726c4d2514c5d9f28b43 to your computer and use it in GitHub Desktop.
Save mul14/726c4d2514c5d9f28b43 to your computer and use it in GitHub Desktop.
Simple Laravel Search Trait

Usage

Put SearchTrait.php in app directory. Then use SearchTrait in your model, like so

use App\SearchTrait;
use Illuminate\Database\Eloquent\Model;

class Article extends Model 
{
    use SearchTrait; // Add this
    
    // Optional properties
    protected $search = ['title', 'content'];

}

Somewhere in your controller

$keyword = 'lorem';

// Match any fields
Article::search($keyword)->paginate();

// Match all fields
Article::search($keyword, true)->paginate();
<?php
namespace App;
use Illuminate\Support\Facades\Schema;
trait SearchTrait
{
/**
* @param \Illuminate\Database\Eloquent\Builder|static $query
* @param string $keyword
* @param boolean $matchAllFields
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function scopeSearch($query, $keyword, $matchAllFields = false)
{
return static::where(function ($query) use ($keyword, $matchAllFields) {
foreach (static::getSearchableFields() as $field) {
if ($matchAllFields) {
$query->where($field, 'LIKE', "%$keyword%");
} else {
$query->orWhere($field, 'LIKE', "%$keyword%");
}
}
});
}
/**
* Get all searchable fields
*
* @return array
*/
public static function getSearchableFields()
{
$model = new static;
$fields = $model->search;
if (empty($fields)) {
$fields = Schema::getColumnListing($model->getTable());
$ignoredColumns = [
$model->getKeyName(),
$model->getUpdatedAtColumn(),
$model->getCreatedAtColumn(),
];
if (method_exists($model, 'getDeletedAtColumn')) {
$ignoredColumns[] = $model->getDeletedAtColumn();
}
$fields = array_diff($fields, $model->getHidden(), $ignoredColumns);
}
return $fields;
}
}
@fer-ri
Copy link

fer-ri commented Aug 31, 2015

Thanks om Mullllll 💃

@nasrulkurniawan
Copy link

mksh Mas Mul.

@mrofi
Copy link

mrofi commented Aug 31, 2015

Keren.. makasih kakak.. 😄 ☕

@Guerzizeb
Copy link

Thanks :)

@ManojKiranA
Copy link

it worked thanks

@tuananh19961
Copy link

Perfect! :)

@mul14
Copy link
Author

mul14 commented Aug 12, 2020

Wow, saya sudah lupa pernah bikin ini, dan ternyata ada komentar masuk dari 2015 😦

@myusufid
Copy link

keren 💯

@syawqi
Copy link

syawqi commented Aug 13, 2020

mantap om

@azazqadir
Copy link

Can i used this in live search that i have created with Laravel and AJAX? Is it gonna work with AJAX?

@mul14
Copy link
Author

mul14 commented Sep 11, 2020

@azazqadir yes, of course.

@saber13812002
Copy link

great. useful. more helpful than 30 minutes cbt clips

i think this is new great idea for developers... instead of hours of video in youtube. we can read it more rapid

@Ir001
Copy link

Ir001 commented Nov 19, 2020

Mantap, sangat memudahkan

@klisica
Copy link

klisica commented Nov 29, 2020

Any idea on how to extend this trait to handle search on relationships? I.e. on belongsTo relationship

@shreekrishnalamichhane
Copy link

Thanks for sharing this..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment