Server side search meta data not accessible

Hi

Using Laravel Scout Extended.

Trying to get at scout meta data using the example provided in docs:

/doc/framework-integration/laravel/searching/server-side-search/?language=php#results-metadata

$metadata = MyModel::search('Star Trek')->get()->first()->scoutMetaData();

This gives an error of:

Call to undefined method MyModel::scoutMetaData()

“MyModel” is a scout “Aggregator”.

MyModel::search('Star Trek')->get() and MyModel::search('Star Trek')->raw() both work fine.

Can’t see why or how this would not be available to an aggregator, or even if this is the actual issue?

Thanks

Tom

Thanks for asking.

Yes, indeed the code to get metadata for the retrieved models is:

$metadata = MyModel::search('Star Trek')->get()->first()->scoutMetadata();

Keep in mind, the method should be scoutMetadata() instead of scoutMetaData().

Note that, the method scoutMetadata() will not be available in non searchable models included in the aggregator. So if the MyModel::search('Star Trek')->get()->first() returns a non searchable model, the scoutMetadata() won’t be available:

class News extends Aggregator
{
    protected $models = [
         // If the model bellow contains the `Searchable` trait, `scoutMetadata()` will work in this model.
        \App\User::class, 

        // If the model bellow do NOT contains the `Searchable` trait, `scoutMetadata()` will NOT work in this model.
        \App\Article::class,
    ];
}

Thanks, let me know if you have any other question.

Ah right, I see thank you, there was currently no trait on that model.

However if I add this trait to the model will that not create a new index when the model save / updates, or does the aggregator override this?

UPDATE: Just tested this and it does create a new “indice” so end up with the aggregator and all the models having their own indice as well. Is there anyway to avoid this behaviour?

Continuation of this thread: https://github.com/algolia/scout-extended/issues/206.