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” 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.
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
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();Build Indexes for existing data
Section titled “Build Indexes for existing data”php artisan lens:build ProfileBuilds (or rebuilds) all IndexedProfile records from your existing Profile data.
