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” 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 theApp\Models\Indexes
directory.
namespace App\Models\Indexes;
use PDPhilip\ElasticLens\IndexModel;
class IndexedProfile extends IndexModel{}
2 (b) Or create with artisan
Section titled “2 (b) Or create with artisan”php artisan lens:make Profile
Generates a new index for the Profile
model.

- That’s it! Your
Profile
model will now automatically sync with theIndexedProfile
model whenever changes occur. You can search your User model effortlessly, like:
Profile::viaIndex()->searchTerm('running')->orSearchTerm('swimming')->get();
Build Indexes for existing data
Section titled “Build Indexes for existing data”php artisan lens:build Profile
Build/Rebuilds all the IndexedProfile
records for the Profile
model.
