繁体   English   中英

在搜索Algolia索引时,可以在eloquent集合中使用Relation数据

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

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

我试图弄清楚如何使用Algolia索引搜索“State”“FoodCat”的关系数据与搜索结果。

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

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

但是,当搜索索引并将结果提取到一个雄辩的集合中时,“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');

但是我想知道是否有办法在不使用paginator类的情况下提供相同的数据...

不太确定我的问题是否正确,但是Eloquent的with()函数可能就是您正在寻找的。

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

渴望在雄辩中加载关系

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM