jhacobs/laravel-searchable

Search through models

v1.0.0 2021-09-19 01:21 UTC

This package is auto-updated.

Last update: 2024-04-19 07:25:53 UTC


README

Packagist Version GitHub PHP Workflow

Search through models with laravel searchable

Installation

You can install the package via composer

  composer require jhacobs/laravel-searchable

Usage/Examples

Prepare your models

Add the Searchable trait to the model you want to search through.

namespace App\Models\User;

use Illuminate\Database\Eloquent\Model;
use Jhacobs\Searchable\Searchable;

class User extends Model
{
    use Searchable;
}

Then add the fields you want to be searchable to the $searchables property.

namespace App\Models\User;

use Illuminate\Database\Eloquent\Model;
use Jhacobs\Searchable\Searchable;

class User extends Model
{
    use Searchable;

    protected $searchables = [
        'name',
        'email'
    ];
}

Searching models

You can search through your models by using the search scope.

User::search('Henk')
    ->get();

Running Tests

To run tests, run the following command

  composer test

License

MIT