karabinse/eloquent-searchable

Easily search eloquent models and their relations

1.2.0 2024-09-06 07:54 UTC

This package is auto-updated.

Last update: 2024-11-06 08:12:29 UTC


README

Latest Version on Packagist GitHub Tests Action Status

Installation

You can install the package via composer:

composer require karabinse/eloquent-searchable

Usage

Add the trait and add columns that are searchable.

use Karabin\Searchable\Concerns\IsSearchable;

class User extends Authenticatable
{
    use HasFactory, HasRoles, IsSearchable, Notifiable;

    protected $searchable = [
        'name',
        'email',
        'posts.title'
    ];

The package also includes a custom TermSearchFitler to be used with Laravel Query Builder.

use Karabin\Searchable\Filters\TermSearchFilter;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $users = QueryBuilder::for(User::class)
            ->allowedFilters([
                AllowedFilter::custom('search', new TermSearchFilter),
            ])
            ->paginate($request->query('limit', 10));

        return Inertia::render('User/Index', [
            'users' => UserData::collect($users),
        ]);
    }

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.