LaravelPackages.net
Acme Inc.
Toggle sidebar
craftcodery/laravel-searchable

Searchable trait for Laravel Eloquent models

7.038
0
3.5.0
About craftcodery/laravel-searchable

craftcodery/laravel-searchable is a Laravel package for searchable trait for laravel eloquent models. It currently has 0 GitHub stars and 7.038 downloads on Packagist (latest version 3.5.0). Install it with composer require craftcodery/laravel-searchable. Discover more Laravel packages by craftcodery or browse all Laravel packages to compare alternatives.

Last updated

Laravel Searchable

This package makes it easy to search your Laravel models.

Installation

You can install the package via composer:

composer require craftcodery/laravel-searchable

Usage

Preparing your models

In order to search through models you'll have to use the Searchable trait and add the toSearchableArray method.

namespace App\Models;

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

class User extends Model
{
    use Searchable;

    /**
     * Get the searchable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'columns' => [
                'users.name'  => 60,
                'users.email' => 60,
                'locations.city' => 40,
                'organizations.name' => 40
            ],
            'joins'   => [
                'locations' => [
                    'users.location_id',
                    'locations.id'
                ],
                'organizations' => [
                    // use commas to join on multiple columns, e.g. where
                    // organizations.id equals primary_org_id OR secondary_or_id
                    'users.primary_org_id,users.secondary_org_id',
                    'organizations.id'
                ]
            ],
            'groupBy' => 'users.id'
        ];
    }
}

Searching models

To search your models, just use the search method.

$users = User::search('john')->get();

Configuring search matchers

You can configure the different search matchers and weights given to each used by the package.

php artisan vendor:publish --tag=searchable-config

License

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

Star History Chart