Skip to content

Indexing your Base-Model

Instant config. Add the Indexable trait to your Base Model then set your Index Model and you’re good to go.


1. Add the Indexable Trait to Your Base-Model

Section titled “1. Add the Indexable Trait to Your Base-Model”
App\Models\Profile.php
use PDPhilip\ElasticLens\Indexable;
class Profile extends Model
{
use Indexable;

2 (a) Create an Index-Model for your Base-Model

Section titled “2 (a) Create an Index-Model for your Base-Model”
  • ElasticLens expects the Index Model to be named as Indexed + BaseModelName and located in the App\Models\Indexes directory.
App\Models\Indexes\IndexedProfile.php
namespace App\Models\Indexes;
use PDPhilip\ElasticLens\IndexModel;
class IndexedProfile extends IndexModel{}
Terminal window
php artisan lens:make Profile

Generates a new index for the Profile model.

ElasticLens Build
  • That’s it! Your Profile model will now automatically sync with the IndexedProfile model whenever changes occur. You can search your User model effortlessly, like:
Profile::viaIndex()->searchTerm('running')->orSearchTerm('swimming')->get();
Terminal window
php artisan lens:build Profile

Build/Rebuilds all the IndexedProfile records for the Profile model.

ElasticLens Build