Laravel Scout / Algolia:模型关系无法建立索引

时间:2020-07-29 09:31:35

标签: php laravel eloquent algolia laravel-scout

我有3个模型-产品,用户和用户产品。 UserProduct是具有特定用户价格的特定产品,该产品的产品代码等的实例。因此,一个产品可以具有多个UserProduct,并且通过这些UserProduct,可以拥有许多用户。这些关系在产品模型中定义:

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

我知道这些关系正在起作用,就像在Artisan Tinker中一样,当将其中任何一个作为属性调用时,我都会得到结果。

在我的Algolia 产品索引中,我希望结果包含有关相关User和UserProducts的信息以进行过滤。因此,我将按照Scout Extended和Algolia文档中的建议加载这些关系:

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

万事俱备,但由于某些原因,“用户”和“用户产品”都空了。

Screenshot of Algolia products index

具有“用户”和“产品”关系的UserProducts在被拉入我的 user_products 索引之前已经成功加载了这些关系,这使这一点更加令人困惑。 / p>

Screenshot of Algolia index

据此,我只能得出结论,问题必须在于如何设置用于填充数据库的工厂文件。对于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
   ];
});

…而在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();
       });
 
   }
});

在afterCreating回调中进行的所有更改均已成功执行。但是,对代码不屑一顾,添加$ product-> searchable()等似乎并没有为我的产品索引带来必要的数据。任何有用的建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

可能是您忘记在产品模型上使用Laravel的touch功能。参见https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?language=php#updating-relations-when-parentchild-change

让我知道这是否可以解决您的问题