Skip to content

Indexing your Base-Model

Two steps. Trait on your Base Model, create your Index Model. Done.


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 Indexed + BaseModelName 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
ElasticLens Build

That’s it. Your Profile model now syncs with IndexedProfile automatically on every create, update, and delete. Search it:

Profile::viaIndex()->searchTerm('running')->orSearchTerm('swimming')->get();
Terminal window
php artisan lens:build Profile

Builds (or rebuilds) all IndexedProfile records from your existing Profile data.

ElasticLens Build