1. Go to this page and download the library: Download masterro/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/ */
class User extends Model implements SearchableContract
{
public function posts() {
return $this->hasMany(Post::class);
}
public function filterSearchResults($query) {
return $query->whereHas('posts', function ($query) {
$query->where('is_published', true);
});
}
}
$result = $this->searchable
->withFilter(function (Builder $query) {
return $query->getModel() instanceof Post
? $query->where('description', '!=', 'Doloremque iure sequi quos sequi consequatur.')
: $query;
})
->search('Dolorem');
$result = $this->searchable->withoutModelFilters()->search('quia est ipsa molestiae hic');
$result = $this->searchable
->withoutModelFilters(Post::class)
->search('quia est ipsa molestiae hic');
$result = $this->searchable
->withoutModelFilters([Article::class, Post::class])
->search('quia est ipsa molestiae hic');