'tntsearch' => [
'storage' => storage_path(), //place where the index files will be stored
'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
'fuzzy' => [
'prefix_length' => 2,
'max_expansions' => 50,
'distance' => 2,
'no_limit' => true
],
'asYouType' => false,
'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
'maxDocs' => env('TNTSEARCH_MAX_DOCS', 500),
],
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public $asYouType = true;
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
// Customize array...
return $array;
}
}
namespace App\Http\Controllers;
use App\Post;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$post = new Post;
// filter out posts to which the given topic is assigned
if($request->topic) {
$post = $post->whereNotIn('id', function($query){
$query->select('assigned_to')->from('comments')->where('topic','=', request()->input('topic'));
});
}
// only posts from people that are no moderators
$post = $post->byRole('moderator','!=');
// when user is not admin filter out internal posts
if(!auth()->user()->hasRole('admin'))
{
$post= $post->where('internal_post', false);
}
if ($request->searchTerm) {
$constraints = $post; // not necessary but for better readability
$post = Post::search($request->searchTerm)->constrain($constraints);
}
$post->where('deleted', false);
$post->orderBy('updated_at', 'asc');
$paginator = $post->paginate(10);
$posts = $paginator->getCollection();
// return posts
}
}
$post = Post::find(1);
// You may also add record via collection...
$post->searchable();
// OR
$posts = Post::where('year', '>', '2018')->get();
// You may also add records via collections...
$posts->searchable();