简体   繁体   English

Laravel Scout/Algolia:Model 关系没有通过索引

[英]Laravel Scout/Algolia: Model relationships not pulling through to index

I have 3 models—Products, Users, and UserProducts.我有 3 个模型——Products、Users 和 UserProducts。 A UserProduct is an instance of a particular product with a User's specific price, product code etc for that item. UserProduct 是特定产品的实例,具有用户对该项目的特定价格、产品代码等。 Consequently one Product can have many UserProducts, and through those UserProducts, many Users.因此,一个产品可以有许多用户产品,并且通过这些用户产品,可以有许多用户。 These relationships are defined within the Product model:这些关系在产品 model 中定义:

public function userProducts() {
       return $this->hasMany(UserProduct::class);
}
 
public function users() {
return $this->belongsToMany(User::class, 'user_products');
}

I know that these relationships are working, as in Artisan Tinker I get results when calling either of these as a property.我知道这些关系正在发挥作用,因为在 Artisan Tinker 中,我将其中任何一个作为属性调用时都会得到结果。

In my Algolia products index, I want results to contain information about related Users and UserProducts for filtering.在我的 Algolia产品索引中,我希望结果包含有关用于过滤的相关用户和用户产品的信息。 Consequently, I'm loading these relationships as recommended in both the Scout Extended and Algolia documentation:因此,我按照 Scout Extended 和 Algolia 文档中的建议加载这些关系:

public function toSearchableArray() {
    $this->users;
    $this->userProducts;
    $array = $this->toArray();
     // Apply Scout Extended default transformations:
    $array = $this->transform($array);
    return $array;
}

Everything is pulling through, but for some reason 'users' and 'user_products' are coming up empty.一切都在顺利进行,但由于某种原因,'users' 和 'user_products' 都变成空的了。

Algolia产品指数截图

This is made all the more confusing by the fact that UserProducts, which have 'user' and 'product' relationships, are successfully loading those relationships prior to them being pulled into my user_products index:具有“用户”和“产品”关系的 UserProducts 在将这些关系拉入我的user_products索引之前成功加载了这些关系,这让情况变得更加混乱:

Algolia指数截图

From this, I can only conclude that the issue must lie with how I've set up the factory files that populate the database.由此,我只能得出结论,问题一定在于我如何设置填充数据库的工厂文件。 In the case of UserProducts I've used a simple factory:对于 UserProducts,我使用了一个简单的工厂:

<?php
 
/** @var \Illuminate\Database\Eloquent\Factory $factory */
 
use App\UserProduct;
use Faker\Generator as Faker;
 
$factory->define(UserProduct::class, function (Faker $faker) {
   return [
       'code' => 'EAN' . $faker->ean8,
       'price' => $faker->randomFloat(2, 5, 1000),
       'product_id' => App\Product::all()->random()->id,
       'user_id' => App\User::where('type', 'supplier')->inRandomOrder()->first()->id
   ];
});

… whereas what happens in the User factory is a bit more complex: ……而在 User 工厂中发生的事情要复杂一些:

$factory->define(User::class, function (Faker $faker) {
   return [
       'display_name' => $faker->name,
       'email' => $faker->unique()->safeEmail,
       'phone' => $faker->phoneNumber,
       'type'  => array_rand(['supplier', 'buyer']),
       'profile_completed' => true,
       'email_verified_at' => now(),
       'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
       'remember_token' => Str::random(10),
   ];
});
 
$factory->afterCreating(User::class, function ($user, $faker) {
 
   $user->assignRole($user->type);
 
   if($user->type==='supplier') {
       // create five products for them
       $products = factory(App\Product::class, 5)->create();
 
       $products->each(function($product) use ($user) {
 
           $user_product = factory(App\UserProduct::class)->create([
               'product_id' => $product->id,
               'user_id' => $user->id
           ]);
 
       })->each(function($product){
           $product->setUserProductCount();
           $product->update();
       });
 
   }
});

All the changes made in the afterCreating callback are performed successfully.在 afterCreating 回调中所做的所有更改都已成功执行。 However, no amount of fiddling with this code, adding $product->searchable() etc seems to bring the necessary data through to my products index.但是,无论对这段代码进行多少改动,添加 $product->searchable() 等似乎都可以将必要的数据带到我的产品索引中。 Any helpful suggestions would be greatly appreciated.任何有用的建议将不胜感激。

It can be that you forgot to use the touch feature of Laravel on your Product model. See https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?language=php#updating-relations-when-parentchild-change可能是您忘记在产品 model 上使用 Laravel 的touch功能。请参阅https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?language=php#更新关系时父子更改

Let me know if this solves your issue让我知道这是否能解决您的问题

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

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