ditscheri/eloquent-search

This is my package EloquentSearch

0.0.5 2024-01-24 13:27 UTC

README

PACKAGE IN DEVELOPMENT, DO NOT USE YET

Eloquent Search

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package lets you perform fast and local searches on your Eloquent Models. You can search foreign columns of related models too.

The approach is based on a gist by Jonathan Reinink (https://gist.github.com/reinink/28bd174087e929ffa1f150e3fe8ffbfa).

Installation

You can install the package via composer:

composer require ditscheri/eloquent-search

Usage

class Podcast extends Model
{
    use \Ditscheri\EloquentSearch\Searchable;

    /**
     * The attributes that are searchable.
     *
     * @var string[]
     */
    protected array $searchable = [
        'title', // make sure to add proper indexes to each of these columns
        'description',
        'author.first_name',
        'author.last_name',
        'comments.message',
        'series.title',
        'series.tags.name',
    ];

    public function author()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function series()
    {
        return $this->belongsTo(Series::class);
    }
}

class PodcastController 
{
    public function index(Request $request)
    {
        return Podcast::search($request->input('q', null))->paginate();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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