Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

spatie/freek.dev

636 stars
3 code files
View spatie/freek.dev on GitHub

composer.json

Open in GitHub
{
//
"require": {
"php": "^8.0",
"algolia/algoliasearch-client-php": "^2.7",
//
"laravel/scout": "^8.3"
},
//
}

app/Models/Post.php

Open in GitHub
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
 
class Post extends Model
{
use Searchable;
//
public function toSearchableArray(): array
{
if (! $this->published) {
return [];
}
 
$postAttributes = $this->toArray();
 
unset($postAttributes['text']);
 
return $postAttributes;
}
//
}

app/Http/Livewire/SearchComponent.php

Open in GitHub
use App\Models\Post;
use Illuminate\Support\Collection;
use Livewire\Component;
 
class SearchComponent extends Component
{
public string $query = '';
 
public function render()
{
return view('livewire.search', [
'results' => $this->getResults(),
]);
}
 
public function getResults(): Collection
{
if ($this->query === '') {
return collect();
}
 
return Post::search($this->query)->take(30)->get();
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.