1. Go to this page and download the library: Download illusionist/searcher library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
illusionist / searcher example snippets
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index(Request $request)
{
return Post::search($request->all())->get();
}
}
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
}
namnespace app\model;
use think\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
}
'body:NULL' // body is null
'not body:null' // body is not null
['body' => null] // body is null
['not' => ['body' => null]] // body is not null
'Apple' // %Apple% like at least one of the searchable columns
'"John Doe"' // %John Doe% like at least one of the searchable columns
'not "John Doe"' // %John Doe% not like any of the searchable columns
['Apple'] // %Apple% like at least one of the searchable columns
['not' => 'John Doe'] // %John Doe% not like any of the searchable columns
'title:Hello body:World' // Implicit and
'title:Hello and body:World' // Explicit and
'title:Hello or body:World' // Explicit or
'A B or C D' // Equivalent to '(A and B) or (C and D)'
'A or B and C or D' // Equivalent to 'A or (B and C) or D'
'(A or B) and (C or D)' // Explicit nested priority
'not (A and B)' // Equivalent to 'not A or not B'
'not (A or B)' // Equivalent to 'not A and not B'
['title' => 'Hello', 'body' => 'World'] // Implicit and
['and' => ['title' => 'Hello', 'body' => 'World']] // Explicit and
['or' => ['title' => 'Hello', 'body' => 'World']] // Explicit or
['or' => [['A', 'B'], ['C', 'D']]] // Equivalent to '(A and B) or (C and D)'
['or' => ['A', ['B', 'C'], 'D']] // Equivalent to 'A or (B and C) or D'
['andOr' => [['A', 'B'], ['C', 'D']]] // Equivalent to '(A or B) and (C or D)'
['not' => ['A', 'B']] // Equivalent to 'not A or not B'
['notOr' => ['A', 'B']] // Equivalent to 'not A and not B'
// Simple "has" check
'comments' // Has comments
'not comments' // Doesn't have comments
'comments = 3' // Has 3 comments
'not comments = 3' // Doesn't have 3 comments
'comments > 10' // Has more than 10 comments
'not comments <= 10' // Same as before
'comments <= 5' // Has 5 or less comments
'not comments > 5' // Same as before
// "WhereHas" check
'comments: (title: Superbe)' // Has comments with the title "Superbe"
'comments: (not title: Superbe)' // Has comments whose titles are different than "Superbe"
'not comments: (title: Superbe)' // Doesn't have comments with the title "Superbe"
'comments: (quality)' // Has comments whose searchable columns match "%quality%"
'not comments: (spam)' // Doesn't have comments marked as spam
'comments: (spam) >= 3' // Has at least 3 spam comments
'not comments: (spam) >= 3' // Has at most 2 spam comments
'comments: (not spam) >= 3' // Has at least 3 comments that are not spam
'comments: (likes < 5)' // Has comments with less than 5 likes
'comments: (likes < 5) <= 10' // Has at most 10 comments with less than 5 likes
'not comments: (likes < 5)' // Doesn't have comments with less than 5 likes
'comments: (likes > 10 and not spam)' // Has non-spam comments with more than 10 likes
// "WhereHas" shortcuts
'comments.title: Superbe' // Same as 'comments: (title: Superbe)'
'not comments.title: Superbe' // Same as 'not comments: (title: Superbe)'
'comments.spam' // Same as 'comments: (spam)'
'not comments.spam' // Same as 'not comments: (spam)'
'comments.likes < 5' // Same as 'comments: (likes < 5)'
'not comments.likes < 5' // Same as 'not comments: (likes < 5)'
// Nested relationships
'comments: (author: (name: John))' // Has comments from the author named John
'comments.author: (name: John)' // Same as before
'comments.author.name: John' // Same as before
// Nested relationships are optimised
'comments.author.name: John and comments.author.age > 21' // Same as: 'comments: (author: (name: John and age > 21))
'comments.likes > 10 or comments.author.age > 21' // Same as: 'comments: (likes > 10 or author: (age > 21))
// Simple "has" check
['comments'] // Has comments
['not' => ['comments']] // Doesn't have comments
['comments' => 3] // Has 3 comments
['not' => ['comments' => 3]] // Doesn't have 3 comments
['comments' => ['>', 10]] // Has more than 10 comments
['not' => ['comments' => ['<=', 10]]] // Same as before
['comments' => ['<=', 5]] // Has 5 or less comments
['not' => ['comments' => ['>', 5]]] // Same as before
// "WhereHas" check
['comments' => ['title' => 'Superbe']] // Has comments with the title "Superbe"
['comments' => ['not' => ['title' => 'Superbe']]] // Has comments whose titles are different than "Superbe"
['not' => ['comments' => ['title' => 'Superbe']]] // Doesn't have comments with the title "Superbe"
['comments' => 'quality'] // Has comments whose searchable columns match "%quality%"
['not' => ['comments' => 'spam']] // Doesn't have comments marked as spam
['comments' => ['spam', ['>=', 3]]] // Has at least 3 spam comments
['not' => ['comments' => ['spam', ['>=', 3]]]] // Has at most 2 spam comments
['comments' => ['not' => 'spam', ['>=', 3]]] // Has at least 3 comments that are not spam
['comments' => ['likes' => ['<', 5]]] // Has comments with less than 5 likes
['comments' => ['likes' => ['<', 5], ['<=', 10]]] // Has at most 10 comments with less than 5 likes
['not' => ['comments' => ['likes' => ['<', 5]]]] // Doesn't have comments with less than 5 likes
['comments' => ['likes' => ['<', 5], 'not' => 'spam']] // Has non-spam comments with more than 10 likes
// Nested relationships
['comments' => ['author' => ['name' => 'John']]] // Has comments from the author named John
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
/**
* Get the columns of the query phrase.
*
* @param string $phrase
* @return array
*/
public function getQueryPhraseColumns($phrase)
{
if (is_numeric($phrase)) {
return ['stars' => '>=', 'comments.stars' => '>='];
}
return ['title'];
}
}
'lonely' // Equivalent to:
$query->where('title', '%lonely%');
'3000' // Equivalent to:
$query->where(function ($query) {
$query->where('stars', '>=', '3000', 'or')
->whereHas('comments', function ($query) {
$query->where('stars', '>=', '3000')
});
});
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
public function comments()
{
return $this->hasMany(Comment::class);
}
}
// Querying Relationship Existence
'comments' // $query->has('comments');
// Counting Related Models
'select:comments_count' // $query->withCount('comments');
// Eager Loading
'select:comments' // $query->select('id')->with('comments');
'select:comments.title' // $query->select('id')->with('comments:id,title')
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
protected $searchable = ['author', 'created_at'];
}
'author:kayson title:hello' // Equivalent to:
$query->where('author', '=', 'kayson');
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
protected $casts = [
'published' => 'boolean',
'created_at' => 'datetime',
];
}
namnespace app\model;
use think\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
protected $type = [
'published' => 'boolean',
'created_at' => 'datetime',
];
}
namnespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illusionist\Searcher\Contracts\Searchable as SearchableContract;
use Illusionist\Searcher\Eloquent\Searchable;
class Post extends Model implements SearchableContract
{
use Searchable;
/**
* Get the real name of the given search column.
*
* @param string $key
* @return string|array
*/
public function getRelaSearchName($key)
{
switch ($key) {
case 'field':
return 'select';
case 'sort':
return 'order_by';
case 'from':
return 'offset';
case 'stars':
return ['stars', 'comments.stars'];
default:
return $key;
}
}
}
'field:id,name' // Equivalent to:
$query->select(['id', 'name']);
'stars:3000' // Equivalent to:
$query->where(function ($query) {
$query->where('stars', '>=', '3000', 'or')
->whereHas('comments', function ($query) {
$query->where('stars', '>=', '3000')
});
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.