Package Data | |
---|---|
Maintainer Username: | vluzrmos |
Package Create Date: | 2015-09-15 |
Package Last Update: | 2021-08-31 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 03:11:20 |
Package Statistics | |
---|---|
Total Downloads: | 163 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
composer require vluzrmos/eloquent-simple-searchable
Put that trait into your model:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Vluzrmos\SimpleSearchable\Eloquent\SimpleSearchableTrait;
class User extends Model
{
use SimpleSearchableTrait;
protected $searchable = [
'field' => 'type'
];
}
The attribute $searchable should contain the index with a column or a related column and the value is a type of the search which includes:
left_text
: Match the left side of the column valueright_text
: Match the right side of the column valueequals
: The searchd text should be equals to the column valuefull_text
: The searched text should be in any position of the searchd column.
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Vluzrmos\SimpleSearchable\Eloquent\SimpleSearchableTrait;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, SimpleSearchableTrait;
protected $searchable = [
'name' => 'full_text',
'posts.title' => 'full_text',
// query deeply into your relations, that relations should exists on the respective models.
'posts.comments.owner.name' => 'full_text'
];
public function posts()
{
return $this->hasMany(Post::class);
}
}
And in your controller or anywhere:
$users = User::search('Jonh')->get();
// or replace the default searchable fields:
$users = User::search('Jonh', ['name' => 'full_text'])->get();
Note: The
search
method is a scope, so you need to use query builder methods likeget
orall
to perform the search and get a collection of the results.