Search code examples
phplaravel-5algolialaravel-scout

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


I am trying to import some existing records to algolia. now using Laravel 5.5 and scout 3.0.0 with algolia-php-sdk. when I execute php artisan scout:import "App\Listings" -v, it throws the following exception

 [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
`

I have included Searchable trait in the model also the toSearchableArray() method to customize the record being inserted.

I have another model called Store. and I can import that model to algolia without any problem. I have gone through this link. but both my models are extending the same basemodel class so the above link didn't helped me either.

any hints on going forward would be greatly appreciated.

EDIT Code Sample

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

Solution

  • I have figured this out myself. The model contained an $indices array/property which is used to denote the algolia index where the record is being inserted. at the same time, it contains a searchableAs() method also which is doing the same task of specifying the algolia index. and that $indices array is populated via the constructor, so in the first look I was not able to reveal the culprit.

    So simply removing the $indices array and the code where the constructor does the initialization of $indices did work for me. But still its unknown that why Scout throws Call to undefined method::Searchable() Exception