technote/laravel-search-helper is a Laravel package for search helper for laravel.
It currently has 2 GitHub stars and 5.618 downloads on Packagist (latest version v0.2.28).
Install it with composer require technote/laravel-search-helper.
Discover more Laravel packages by technote
or browse all Laravel packages to compare alternatives.
Last updated
Read this in other languages: English, 日本語.
Search helper for Laravel.
composer require technote/laravel-search-helper
Implement Searchable Contract and Searchable Trait.
Implement setConditions method.
<?php
namespace App\Models;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Technote\SearchHelper\Models\Contracts\Searchable as SearchableContract;
use Technote\SearchHelper\Models\Traits\Searchable;
/**
* Class Item
* @mixin Eloquent
*/
class Item extends Model implements SearchableContract
{
use Searchable;
/**
* @var array
*/
protected $guarded = [
'id',
];
/**
* @param Builder $query
* @param array $conditions
*/
protected static function setConditions(Builder $query, array $conditions)
{
if (! empty($conditions['s'])) {
collect($conditions['s'])->each(function ($search) use ($query) {
$query->where(function ($builder) use ($search) {
/** @var Builder $builder */
$builder->where('items.name', 'like', "%{$search}%");
});
});
}
}
}
Call search method.
<?php
use App\Models\Item;
Item::search([
's' => [
'test',
],
'ids' => [1, 2, 3],
])->get();