Skip to content

Commit

Permalink
Add Searchable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhuisinga committed Apr 28, 2021
1 parent cfe7273 commit 63505c4
Show file tree
Hide file tree
Showing 6 changed files with 793 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# laravel-searchable
Searchable trait for Laravel Eloquent models
# Laravel Searchable

This package makes it easy to search your Laravel models.

## Installation

You can install the package via composer:

```bash
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.

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use CraftCodery\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,
],
'joins' => [
'locations' => [
'users.location_id',
'locations.id'
],
],
'groupBy' => 'users.id'
];
}
}
```

### Searching models

To search your models, just use the `search` method.

```php
$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 --provider=CraftCodery\Searchable\SearchableServiceProvider --tag="config"
```

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "craftcodery/laravel-searchable",
"description": "Searchable trait for Laravel Eloquent models",
"keywords": [
"laravel",
"eloquent",
"search"
],
"homepage": "https://github.com/craftcodery/laravel-searchable",
"license": "MIT",
"authors": [
{
"name": "Craft Codery"
}
],
"require": {
"php": "^7.2|^8.0",
"laravel/framework": "^6.0|^7.0|^8.0"
},
"autoload": {
"psr-4": {
"CraftCodery\\Searchable\\": "src/"
}
}
}
16 changes: 16 additions & 0 deletions config/searchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [
'matchers' => [
'exactFullMatcher' => 100,
'exactInStringMatcher' => 100,
'exactMatcher' => 60,
'startOfStringMatcher' => 50,
'acronymMatcher' => 42,
'consecutiveCharactersMatcher' => 40,
'startOfWordsMatcher' => 35,
'inStringMatcher' => 30,
'similarStringMatcher' => 30,
'timesInStringMatcher' => 8,
],
];
Loading

0 comments on commit 63505c4

Please sign in to comment.