1. Go to this page and download the library: Download spatie/laravel-searchable 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/ */
spatie / laravel-searchable example snippets
$searchResults = (new Search())
->registerModel(User::class, 'name')
->registerModel(BlogPost::class, 'title')
->search('john');
namespace Spatie\Searchable;
interface Searchable
{
public function getSearchResult(): SearchResult;
}
use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;
class BlogPost extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
$url = route('blogPost.show', $this->slug);
return new \Spatie\Searchable\SearchResult(
$this,
$this->title,
$url
);
}
}
$searchResults = (new Search())
->registerModel(User::class, 'name')
->search('john');
// use multiple model attributes
$searchResults = (new Search())
->registerModel(User::class, 'first_name', 'last_name')
->search('john');
// or use an array of model attributes
$searchResults = (new Search())
->registerModel(User::class, ['first_name', 'last_name'])
->search('john');
$search = (new Search())
->registerModel(User::class, function(ModelSearchAspect $modelSearchAspect) {
$modelSearchAspect
->addSearchableAttribute('name') // return results for partial matches on usernames
->addExactSearchableAttribute('email') // only return results that exactly match the e-mail address
->active()
->has('posts')
->with('roles');
});
class OrderSearchAspect extends SearchAspect
{
public function getResults(string $term): Collection
{
return OrderApi::searchOrders($term);
}
}
$searchResults = (new Search())
->registerAspect(OrderSearchAspect::class)
->search('john');
$searchResults = (new Search())
->registerAspect(BlogPostAspect::class)
->limitAspectResults(50)
->search('How To');
class BlogPost extends Model implements Searchable
{
public $searchableType = 'custom named aspect';
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.