7 months ago

Scout toSearchableArray doesn't get relations when save from Nova

Posted 7 months ago by msassa

I have a model, with some relations. One is a belongsTo, and I'm adding some information from this relation to the toSearchableArray to indexed in Algolia. The main model is something like this:

class Car extends Model
    use Searchable;

    public function toSearchableArray()
        $searchableCar = array_only(
            ['id', 'title', 'url', 'active', 'vin', 'stock', 'bodyStyle', 'certifiedPreowned', 'color', 'diff', 'engine', 'kilometer', 'miles', 'make', 'model', 'price', 'year']

        $searchableCar['import'] = $this->import->name;

        return $searchableCar;

    public function import()
        return $this->belongsTo(Import::class);

And when I create a new model from Nova, I get an error saying ErrorException: Trying to get property 'name' of non-object like the relation doesn't exist yet.

I make a dump of the model in the saved event, and the import is null ?‍♂️

I fix this overwriting the method:

public function queueMakeSearchable($models)
        if ($models->isEmpty()) {


        if (!config('scout.queue')) {
            return $models->first()->searchableUsing()->update($models);

        dispatch((new MakeSearchable($models))

and manually loading the relation and it works ... but doesn't feel ok

Someone has deal with this problem?

Please sign in or create an account to participate in this conversation.