Php 在搜索Algolia索引时,使关系数据在雄辩的集合中可用
我有一个Laravel 5.7应用程序,使用Scout Extended集成Algolia搜索功能。在应用程序中,我有一个名为“Prospect”的模型,它与其他两个模型有关系:“State”和“FoodCat” 我试图弄清楚在使用Algolia索引进行搜索时,如何将“State”和“FoodCat”的关系数据包含在搜索结果中 通过在Prospect模型中将扩展到searchablearray(),关系数据已包含在搜索索引中 [ 但是,关系数据在搜索后无法在雄辩的集合中使用。它只能通过循环原始数据数组来使用Php 在搜索Algolia索引时,使关系数据在雄辩的集合中可用,php,laravel,laravel-5,algolia,laravel-scout,Php,Laravel,Laravel 5,Algolia,Laravel Scout,我有一个Laravel 5.7应用程序,使用Scout Extended集成Algolia搜索功能。在应用程序中,我有一个名为“Prospect”的模型,它与其他两个模型有关系:“State”和“FoodCat” 我试图弄清楚在使用Algolia索引进行搜索时,如何将“State”和“FoodCat”的关系数据包含在搜索结果中 通过在Prospect模型中将扩展到searchablearray(),关系数据已包含在搜索索引中 [ 但是,关系数据在搜索后无法在雄辩的集合中使用。它只能通过循环原始数
查看原始数据数组,输出原始数据时,搜索索引中既有“State”也有“FoodCat”关系数据: 例如:Prospect::search(“”)->raw()
但是,当搜索索引并将结果提取到有说服力的集合中时,“State”和“FoodCat”数据不可用 …示例输出:Prospect::search(“”)->get() 在搜索Algolia索引后,如何使相同的关系数据可用于Elounce collection?
引用此链接[ …您可以通过paginator访问关系数据,方法是在搜索调用后立即加载关系
$prospects = Prospect::search('pizza')->paginate(999999);
$prospects->load('state');
$prospects->load('foodcat');
然而,我想知道是否有一种方法可以在不使用paginator类的情况下提供相同的数据…不太确定我是否正确回答了您的问题,但可能您正在寻找的是Eloquent的
with()
函数
在你给出的例子中,你可以称之为
Prospect::with('state','foodcat')->get()
以便检索相关模型
Eager以这种方式加载关系效果很好,但在本例中,您不是从Algolia搜索索引中获取,而是直接从数据库中获取。当您尝试以与Algolia相同的方式使用这种方法时,即Prospect::with()->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 [▶]
}
$prospects = Prospect::search('pizza')->paginate(999999);
$prospects->load('state');
$prospects->load('foodcat');