搜索Algolia索引时,可以有条理地收集Relation数据

时间:2019-03-20 16:33:09

标签: php laravel laravel-5 algolia laravel-scout

我有一个 Laravel 5.7 应用程序,该应用程序使用 Scout Extended 集成了 Algolia 搜索功能。在应用程序中,我有一个名为“前景” 的模型,该模型与其他两个模型具有 belongsTo 关系:“州” “ FoodCat” “

我正在尝试找出在使用Algolia索引进行搜索时如何将“州” “ FoodCat” 的关系数据包括在搜索结果中。

通过在Prospect模型中扩展 toSearchableArray(),关系数据已包含在搜索索引中。

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

但是,搜索后,关系数据在Eloquent集合中不可用。它只能通过遍历原始数据数组来使用...


查看原始数据数组,输出原始数据时,“州” “ FoodCat” 关系数据在搜索索引中均可用:< / p>

...以下示例:前景:: search('')->原始()

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 [▶]
    ]

但是,当搜索索引并将结果提取到有说服力的集合中时,“状态”和“食品猫”数据不可用...

...示例输出:前景:: 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 [▶]
    }

搜索Algolia索引后,如何使相同的关系数据可用于雄辩的收藏集中?


引用此链接[https://gist.github.com/Artistan/fea3e21f149fdf845e530299bcff37d4]

...您可以通过分页器访问关系数据,方法是在搜索调用之后急于加载关系...

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

但是我想知道是否有一种方法可以在不使用分页器类的情况下提供相同的数据...

1 个答案:

答案 0 :(得分:0)

不太确定我是否正确回答了您的问题,但是也许Eloquent的with()函数正是您要寻找的东西。

因此,在您给出的示例中,您将调用类似 Prospect::with('state','foodcat')->get() 以便检索相关模型。

Eager Loading Relationships in Eloquent