Making Relation data available in eloquent collection when searching a Algolia index

I have a Laravel 5.7 application using Scout Extended to integrate Algolia search functionality. In the application I have a model called "Prospect" that has belongsTo relationships with two other Models: "State" & "FoodCat".

I am trying to figure out how to include the relationship data for "State" & "FoodCat" with the search results when searching using the Algolia index.

The relationship data has been included in the search index by extending toSearchableArray() in the Prospect model.

[https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/#relationships]

However, the relationship data is not available in the Eloquent collection after searching. It is only available by looping through the raw data array...


Looking at the raw data array, both the "State" and the "FoodCat" relation data is available in the search index when the raw data is outputted:

...out example of: Prospect::search('')->raw()

array:9 [▼
  "hits" => array:20 [▼
    0 => array:33 [▼
      "id" => 251
      "name" => "Pizza House & Grille"
      "contact_fname" => null
      "contact_lname" => null
      "contact_title" => null
      "food_cat_id" => 1
      "phone" => 6195610325
      "fax" => null
      "website" => ""
      "address" => "12580 Lakeshore Drive"
      "address2" => null
      "city" => "Lakeside"
      "state_id" => 5
      "zip" => 92040
      "region_id" => 1
      "latitude" => 38.975201
      "longitude" => -122.676003
      "sic_desc" => "Pizza Restaurants"
      "lead_source" => "Manual"
      "owner_type" => null
      "contacted" => 0
      "contacted_timestamp" => null
      "response_notes" => "Lorem Ipsum Something Something"
      "user_id" => null
      "created_at" => 1319545964
      "updated_at" => 1550950060
      "foodcat" => array:3 [▼
        "id" => 1
        "title" => "Pizza/Italian"
        "slug" => "Pizza-Italian"
      ]
      "state" => array:4 [▼
        "id" => 5
        "name" => "California"
        "abbr" => "CA"
        "active" => 1
      ]
      "_geoloc" => array:2 [▶]
      "food_cat_title" => "Pizza/Italian"
      "_tags" => array:1 [▶]
      "objectID" => "App\Prospect::251"
      "_highlightResult" => array:9 [▶]
    ]

However when searching the index and fetching the results into an eloquent collection the "State" & "FoodCat" data is not available...

...example output of: Prospect::search('')->get()

Collection {#650 ▼
  #items: array:20 [▼
    0 => Prospect {#713 ▼
      #guarded: []
      #connection: "mysql"
      #table: "prospects"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:26 [▶]
      #original: array:26 [▼
        "id" => 333
        "name" => "test"
        "contact_fname" => "test"
        "contact_lname" => "test"
        "contact_title" => "test"
        "food_cat_id" => 1
        "phone" => "6195551212"
        "fax" => ""
        "website" => ""
        "address" => "123 mian"
        "address2" => ""
        "city" => "sd"
        "state_id" => 5
        "zip" => 92101
        "region_id" => 1
        "latitude" => "32.785301"
        "longitude" => "-117.111000"
        "sic_desc" => "Pizza"
        "lead_source" => "Manual"
        "owner_type" => ""
        "contacted" => 0
        "contacted_timestamp" => null
        "response_notes" => ""
        "user_id" => null
        "created_at" => "2019-03-19 11:08:57"
        "updated_at" => "2019-03-19 11:08:57"
      ]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #scoutMetadata: array:1 [▶]
    }

How can I make the same relation data available to the eloquent collection after searching the Algolia index?


Referencing this link [https://gist.github.com/Artistan/fea3e21f149fdf845e530299bcff37d4]

...you can access the relation data through the paginator, by eager loading the relations after the search call...

$prospects = Prospect::search('pizza')->paginate(999999);
$prospects->load('state');
$prospects->load('foodcat');

However I was wondering if there was a way to make the same data available without using the paginator class...

Upvotes: 2

Views: 795

Answers (1)

mynd
mynd

Reputation: 794

Not quite sure whether I got your question right, but maybe the with() function of Eloquent is what you are looking for.

So in your given example you would call something like Prospect::with('state','foodcat')->get() in order to retrieve the related models.

Eager Loading Relationships in Eloquent

Upvotes: 0

Related Questions