PHP code example of mawebcoder / laravel-elasticsearch
1. Go to this page and download the library: Download mawebcoder/laravel-elasticsearch library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mawebcoder / laravel-elasticsearch example snippets
class ArticleCategoryBridge implements SyncInstructionsBridge
{
public function id(): string
{
return 'category.id';
}
public static function model(): string
{
return Category::class;
}
public function updateInstruction(Model $updatedEntity)
{
return [
'category.name' => $updatedEntity->{Category::COLUMN_NAME}
];
}
}
class ArticleCategoryBridge implements CustomSyncBridge
{
public function id(): string
{
return 'category.id';
}
public static function model(): string
{
return Category::class;
}
public function updateDocument(BaseElasticsearchModel $document, Model $updatedEntity)
{
$document->update(['category.title' => $updatedEntity->name];
// Or any custom activity that you want to do!
}
}
class Article extends BaseModel implements ShouldModelSyncByDataBridges
{
public static function getDataBridges() : string|array
{
return CategoryDataBridge::class;
}
}
'bridges' => [
'elastic-model-paths' => [
'app/Elasticsearch/Models',
'infrastructure/*/Elasticsearch/Models', // 👈️ Add your custom path pattern like this (using wildcard)
]
]
//app/Elasticsearch/Migrations
use Mawebcoder\Elasticsearch\Migration\BaseElasticMigration
use App\Elasticsearch\Models\EArticleModel;
return new class extends BaseElasticMigration {
public function getModel():string
{
return EArticleModel::class;
}
public function schema(BaseElasticMigration $mapper): void
{
$mapper->integer('id');
$mapper->string('name');
$mapper->boolean('is_active');
$mapper->text('details');
$mapper->integer('age');
$mapper->object('user',function($BaseElasticMigration $mapper){
return $mapper->string('name')
->object('values',function($mapper){
return $mapper->bigInt('id);
})
});
}
};