Laravel Scout Meilisearch - Role, Creator, and Updater Attributes Missing After Import
Environment:
- Laravel: 12.0
- Scout: 10.13
- Scout Driver: Meilisearch
I'm using spatie/laravel-permissions
for role and permission management.
I have a datatable that shows the list of users with basic user model details along with role
, creator (created_by)
, and updater (updated_by)
columns.
Meilisearch Config (config/scout.php
)
'meilisearch' => [
'host' => env('MEILISEARCH_HOST', 'localhost:7700'),
'key' => env('MEILISEARCH_KEY'),
'index-settings' => [
'users' => [
'filterableAttributes' => User::getFilterableAttributes(),
'sortableAttributes' => User::getSortableAttributes(),
],
],
],
User Model
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable, HasRoles, SoftDeletes, HasUserStamps, Searchable, BelongsToTenant;
protected $fillable = [
'name',
'email',
'password',
'status',
'anization_id',
];
protected $hidden = [
'password',
'remember_token',
];
protected $with = [
'roles',
'creator',
'updater'
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'status' => 'boolean',
];
}
public function employee(): HasOne
{
return $this->hasOne(Employee::class);
}
public function getRoleAttribute()
{
return $this->roles->first() ? $this->roles->first()->name : null;
}
public function makeSearchableUsing(Collection $models): Collection
{
return $models->load(['roles', 'creator', 'updater']);
}
public function toSearchableArray()
{
return [
'id' => (string) $this->id,
'name' => $this->name,
'email' => $this->email,
'roles' => $this->roles->pluck('name')->toArray(),
'role' => $this->roles->first() ? $this->roles->first()->name : null,
'status' => $this->status ? 'Active' : 'Inactive',
'created_at' => $this->created_at->timestamp,
'created_by' => $this->creator ? $this->creator->name : null,
'updated_at' => $this->updated_at->timestamp,
'updated_by' => $this->updater ? $this->updater->name : null,
'anization_id' => $this->anization_id,
];
}
public static function getFilterableAttributes(): array
{
return [
'role',
'status',
'created_by',
'updated_by',
'anization_id',
];
}
public static function getSortableAttributes(): array
{
return [
'id',
'name',
'email',
'roles',
'role',
'status',
'created_at',
'updated_at',
];
}
}
The Issue:
- The role attribute is derived from the getRoleAttribute function.
- The creator and updater attributes come from the HasUserStamps trait, which sets created_by and updated_by values and includes two BelongsTo relationships.
- After importing users into Meilisearch using:
php artisan scout:import "App\Models\User"
The users are successfully imported, but role
, creator
, and updater
attributes appear as null.
Am I missing something? Why are these attributes not being indexed properly? Any insights would be appreciated!
Laravel Scout Meilisearch - Role, Creator, and Updater Attributes Missing After Import
Environment:
- Laravel: 12.0
- Scout: 10.13
- Scout Driver: Meilisearch
I'm using spatie/laravel-permissions
for role and permission management.
I have a datatable that shows the list of users with basic user model details along with role
, creator (created_by)
, and updater (updated_by)
columns.
Meilisearch Config (config/scout.php
)
'meilisearch' => [
'host' => env('MEILISEARCH_HOST', 'localhost:7700'),
'key' => env('MEILISEARCH_KEY'),
'index-settings' => [
'users' => [
'filterableAttributes' => User::getFilterableAttributes(),
'sortableAttributes' => User::getSortableAttributes(),
],
],
],
User Model
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable, HasRoles, SoftDeletes, HasUserStamps, Searchable, BelongsToTenant;
protected $fillable = [
'name',
'email',
'password',
'status',
'anization_id',
];
protected $hidden = [
'password',
'remember_token',
];
protected $with = [
'roles',
'creator',
'updater'
];
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'status' => 'boolean',
];
}
public function employee(): HasOne
{
return $this->hasOne(Employee::class);
}
public function getRoleAttribute()
{
return $this->roles->first() ? $this->roles->first()->name : null;
}
public function makeSearchableUsing(Collection $models): Collection
{
return $models->load(['roles', 'creator', 'updater']);
}
public function toSearchableArray()
{
return [
'id' => (string) $this->id,
'name' => $this->name,
'email' => $this->email,
'roles' => $this->roles->pluck('name')->toArray(),
'role' => $this->roles->first() ? $this->roles->first()->name : null,
'status' => $this->status ? 'Active' : 'Inactive',
'created_at' => $this->created_at->timestamp,
'created_by' => $this->creator ? $this->creator->name : null,
'updated_at' => $this->updated_at->timestamp,
'updated_by' => $this->updater ? $this->updater->name : null,
'anization_id' => $this->anization_id,
];
}
public static function getFilterableAttributes(): array
{
return [
'role',
'status',
'created_by',
'updated_by',
'anization_id',
];
}
public static function getSortableAttributes(): array
{
return [
'id',
'name',
'email',
'roles',
'role',
'status',
'created_at',
'updated_at',
];
}
}
The Issue:
- The role attribute is derived from the getRoleAttribute function.
- The creator and updater attributes come from the HasUserStamps trait, which sets created_by and updated_by values and includes two BelongsTo relationships.
- After importing users into Meilisearch using:
php artisan scout:import "App\Models\User"
The users are successfully imported, but role
, creator
, and updater
attributes appear as null.
Am I missing something? Why are these attributes not being indexed properly? Any insights would be appreciated!
Share Improve this question edited Mar 24 at 3:05 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Mar 24 at 2:46 Mursal KhanMursal Khan 451 silver badge9 bronze badges1 Answer
Reset to default 0The toSearchableArray
function might not automatically load all necessary data.
To ensure your data is ready for indexing, modify your import query to eager load the required relationships.
Refer to the Laravel Scout documentation for details: https://laravel/docs/11.x/scout#modifying-the-import-query
Your makeAllSearchableUsing
function should be updated to include the necessary eager loading:
use Illuminate\Database\Eloquent\Builder;
/**
* Modify the query used to retrieve models when making all of the models searchable.
*/
protected function makeAllSearchableUsing(Builder $query): Builder
{
return $query->with(['roles', 'creator', 'updater']);
}