0

我正在尝试将一些现有记录导入到 algolia。现在使用Laravel 5.5scout 3.0.0与 algolia-php-sdk 一起使用。当我执行时php artisan scout:import "App\Listings" -v,它会引发以下异常

 [BadMethodCallException]                                                  
  Call to undefined method Illuminate\Database\Query\Builder::searchable()  


Exception trace:
 () at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2457
 Illuminate\Database\Query\Builder->__call() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1273
 Illuminate\Database\Eloquent\Builder->__call() at /home/vagrant/Code/vendor/laravel/scout/src/Searchable.php:102
 App\Listings::makeAllSearchable() at /home/vagrant/Code/vendor/laravel/scout/src/Console/ImportCommand.php:43
 Laravel\Scout\Console\ImportCommand->handle() at n/a:n/a
 call_user_func_array() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29
 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:87
 Illuminate\Container\BoundMethod::callBoundMethod() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:31
 Illuminate\Container\BoundMethod::call() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Container/Container.php:549
 Illuminate\Container\Container->call() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php:180
 Illuminate\Console\Command->execute() at /home/vagrant/Code/vendor/symfony/console/Command/Command.php:262
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Command.php:167
 Illuminate\Console\Command->run() at /home/vagrant/Code/vendor/symfony/console/Application.php:888
 Symfony\Component\Console\Application->doRunCommand() at /home/vagrant/Code/vendor/symfony/console/Application.php:224
 Symfony\Component\Console\Application->doRun() at /home/vagrant/Code/vendor/symfony/console/Application.php:125
 Symfony\Component\Console\Application->run() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Console/Application.php:88
 Illuminate\Console\Application->run() at /home/vagrant/Code/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:121
 Illuminate\Foundation\Console\Kernel->handle() at /home/vagrant/Code/artisan:35
`

Searchable在模型中包含了 trait 以及toSearchableArray()自定义插入记录的方法。

我有另一个模型叫做Store. 我可以毫无问题地将该模型导入 algolia。我已经通过这个链接。但是我的两个模型都扩展了相同的 basemodel 类,所以上面的链接也没有帮助我。

任何有关前进的提示将不胜感激。

编辑 代码示例

namespace App;

use App\Services\Search\Builders\ListingRecordBuilder;

use Laravel\Scout\Searchable;
.
.
.

class Listings extends BaseModel
{
    use Searchable;
.
.
.
    public function searchableAs()
    {
        return 'local_listings_new';
    }

    public function toSearchableArray()
    {
        $recordBuilder = new ListingRecordBuilder($this);

        return $recordBuilder->buildRecord();
    }
}
4

1 回答 1

1

我自己已经弄清楚了。该模型包含一个$indices数组/属性,用于表示插入记录的 algolia 索引。同时,它还包含一个searchableAs()方法,该方法也在执行指定 algolia 索引的相同任务。并且该$indices数组是通过构造函数填充的,因此乍一看我无法揭示罪魁祸首。

所以简单地删除$indices数组和构造函数初始化的代码$indices对我有用。但仍然未知 Scout 为什么抛出Call to undefined method::Searchable()异常

于 2017-10-21T06:15:06.463 回答