PHP code example of webikevn / laravel-fulltext-search
1. Go to this page and download the library: Download webikevn/laravel-fulltext-search 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/ */
webikevn / laravel-fulltext-search example snippets
use Webike\Searchable\Searchable;
class Post
{
use Searchable;
protected $searchable = [
// This will search on the defined searchable columns
'columns' => [
'posts.title',
'posts.body',
'author_full_name' => 'CONCAT(authors.first_name, " ", authors.last_name)'
],
'joins' => [
'authors' => ['authors.id', 'posts.author_id']
]
];
public function author()
{
return $this->belongsTo(Author::class);
}
}
// Usage
Post::search("Some title or body content or even the author's full name")
->with('author')
->paginate();
class PostsController
{
public function index()
{
return Post::sortByRelevance(!request()->bool('sort_by'))
->search(request('search'))
->when(Post::isColumnValid($sortColumn = request('sort_by')), function ($query) use ($sortColumn) {
$query->orderBy(
\DB::raw(
(new Post)->getSortableColumn($sortColumn) ?? // valid sortable column
(new Post)->searchQuery()->getColumn($sortColumn) ?? // valid search column
$sortColumn // valid original table column
),
request()->bool('descending') ? 'desc' : 'asc'
);
})
->paginate();
}
}
use Webike\Searchable\Searchable;
class Post extends Model
{
use Searchable;
/**
* Searchable model definitions.
*/
protected $searchable = [
// Searchable columns of the model.
// If this is not defined it will default to all table columns.
'columns' => [
'posts.title',
'posts.body',
'author_full_name' => 'CONCAT(authors.first_name, " ", authors.last_name)'
],
// This is needed if there is a need to join other tables for derived columns.
'joins' => [
'authors' => ['authors.id', 'posts.author_id'], // defaults to leftJoin method of eloquent builder
'another_table' => ['another_table.id', 'authors.another_table_id', 'join'], // can pass leftJoin, rightJoin, join of eloquent builder.
]
];
/**
* Can also be written like this for searchable columns.
*
* @var array
*/
protected $searchableColumns = [
'title',
'body',
'author_full_name' => 'CONCAT(authors.first_name, " ", authors.last_name)'
];
/**
* Can also be written like this for searchable joins.
*
* @var array
*/
protected $searchableJoins = [
'authors' => ['authors.id', 'posts.author_id']
];
}
// Usage
// Call search anywhere
// This only search on the defined columns.
Post::search('Some post')->paginate();
Post::where('likes', '>', 100)->search('Some post')->paginate();
$post = new Post;
$post->setSearchable([ // addSearchable() method is also available
'columns' => [
'posts.title',
'posts.body',
],
'joins' => [
'authors' => ['authors.id', 'posts.author_id']
]
]);
// or
$post->setSearchableColumns([ // addSearchableColumns() method is also available
'posts.title',
'posts.body',
]);
$post->setSearchableJoins([ // addSearchableJoins() method is also available
'authors' => ['authors.id', 'posts.author_id']
]);
class Post {
protected $searchable = [
'columns' => [
'title' => 'posts.title',
],
'sortable_columns' => [
'status_name' => 'statuses.name',
],
'joins' => [
'statuses' => ['statuses.id', 'posts.status_id']
]
];
}
// Usage
Post::search('A post title')->orderBy(Post::getSortableColumn('status_name'));
// This will only perform search on `posts`.`title` column and it will append "order by `statuses`.`name`" in the query.
// This is beneficial if your column is mapped to a different column name coming from front-end request.
namespace App;
use Webike\Searchable\Search\SublimeSearch;
class ExactSearch extends SublimeSearch
{
/**
* {@inheritDoc}
*/
protected function parseSearchStr($searchStr)
{
return $searchStr; // produces "where `column` like '{$searchStr}'"
// or
return "%{$searchStr}%"; // produces "where `column` like '%{$searchStr}%'"
}
}
namespace App;
class User extends Model
{
public function defaultSearchQuery()
{
return new ExactSearch($this, $this->searchableColumns(), $this->sortByRelevance, 'where');
}
}