2

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

我试图弄清楚在使用 Algolia 索引进行搜索时如何将“State”“FoodCat”的关系数据包含在搜索结果中。

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

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

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


查看原始数据数组,当原始数据输出时, “State”“FoodCat”关系数据都在搜索索引中可用:

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

但是,当搜索索引并将结果提取到一个 eloquent 集合中时,“State”和“FoodCat”数据不可用......

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

搜索 Algolia 索引后,如何使相同的关系数据可用于 eloquent 集合?


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

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

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

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

4

1 回答 1

0

不太确定我的问题是否正确,但也许with()Eloquent 的功能正是您想要的。

因此,在您给定的示例中,您会调用类似的东西 Prospect::with('state','foodcat')->get() 来检索相关模型。

Eloquent 中的急切加载关系

于 2019-03-20T18:22:26.357 回答