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

Model文件:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use ScoutElastic\Searchable;

class Asset extends Model
{
    use Searchable;
    use Traits\ImportExcel;
    use Traits\DataHandling;
    use Traits\GenerateReport;

    //指定表名
    protected $table = 'assets_target';

    public $timestamps = false;

    protected $indexConfigurator = AssetIndexConfigurator::class;

    protected $mapping = [
        'properties' => [
            'address' => [
                'type' => 'text',
                'analyzer' => 'ik_smart'
            ],
        ]
    ];

    public function toSearchableArray()
    {
        return [
            'address'=> $this->address
        ];
    }

AssetIndexConfigurator文件:

<?php

namespace App\Models;

use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

class AssetIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    /**
     * @var array
     */
    protected $settings = [
        //
    ];
}

.env

SCOUT_DRIVER=elastic
SCOUT_ELASTIC_HOST=elasticsearch:9200

file
file

报错如下:

file

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
liyu001989
最佳答案

boot 方法会调用每个 trait 里面的 bootTrait 方法,做一些注册工作,例如这个 searchable 的 scope。

https://github.com/laravel/scout/blob/7.0/... 默认使用了 chunk 了,普通的也就是上线的整体导入一次,通常可以接受。量特别大的话,想办法多个服务器分批同时导入吧。

5年前 评论
讨论数量: 5
liyu001989

提供一下 完整的模型代码,是不是重写了 boot 方法,但是没有调用 parent::boot()

5年前 评论

@liyu001989 没有,就是简单的模型,刚才把别的全部注释了还是报这个错,也是按着教程来的,不过其他模型好像可以

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use ScoutElastic\Searchable;

class Asset extends Model
{
    use Searchable;
    use Traits\ImportExcel;
    // use Traits\DataHandling;
    // use Traits\GenerateReport;

    //指定表名
    protected $table = 'assets_target';

    // public $timestamps = false;

    protected $indexConfigurator = AssetIndexConfigurator::class;

    protected $mapping = [
        'properties' => [
            'target_address' => [
                'type' => 'text',
                'analyzer' => 'ik_smart'
            ],
            'target_department' => [
                'type' => 'keyword',
            ],
            'target_system' => [
                'type' => 'keyword',
            ],
            'target_user' => [
                'type' => 'keyword',
            ],
            'target_scan_time' => [
                'type' => 'keyword',
            ],
            'target_check_time' => [
                'type' => 'keyword',
            ],
            'target_important' => [
                'type' => 'keyword',
            ],
            'target_type' => [
                'type' => 'keyword',
            ],
            'target_alive' => [
                'type' => 'keyword',
            ],
        ]
    ];

    // public function toSearchableArray()
    // {
    //     return [
    //         'target_address'=> $this->target_address,
    //         'target_department' => $this->departments->target_department,
    //         'target_system' => $this->systems->target_system,
    //         'target_user' => $this->users->target_user,
    //         'target_scan_time' => $this->target_scan_time,
    //         'target_check_time' => $this->target_check_time,
    //         'target_important' => $this->target_important,
    //         'target_type' => $this->target_type,
    //         'target_alive' => $this->target_alive,
    //     ];
    // }

    public function systems()
    {
        return $this->belongsTo('App\Models\System','id','target_system');
    }

    public function departments()
    {
        return $this->belongsTo('App\Models\Department','id','target_department');
    }

    public function users()
    {
        return $this->belongsTo('App\Models\User','id','target_user');
    }

    // public function vulnBatchs()
    // {
    //     return $this->hasOne('App\Models\VulnBatch','target_hashid','target_hashid');
    // }

    // public function normalVulnBatchs()
    // {
    //     return $this->hasOne('App\Models\VulnBatch','target_address','target_address');
    // }

    // public function assetsBatchs()
    // {
    //     return $this->hasOne('App\Models\AssetsBatch','target_address','target_address');
    // }

    // public function vulnReports()
    // {
    //     return $this->hasMany('App\Models\VulnReport','target_hashid','target_hashid');
    // }

    // //获取漏洞数量
    // public function getVulnNums($target_hashid)
    // {
    //     return VulnReport::where('target_hashid',$target_hashid)->select(\DB::raw('COUNT(*) as count, vuln_rank'))->groupBy(\DB::Raw('vuln_rank'))->pluck('count', 'vuln_rank')->toArray();
    // }

}
5年前 评论

@liyu001989 发现问题了,上面这个评论里的ImportExcel这个 traits忘记注释了,现在traits都注释了,可以上传了,为什么会受traits的影响。。。还有个问题要请教下,php artisan scout:import "App\Topic",这个命令是一次性上传整张表数据,如果量很大就很慢了,正常是怎么操作的?

5年前 评论
liyu001989

boot 方法会调用每个 trait 里面的 bootTrait 方法,做一些注册工作,例如这个 searchable 的 scope。

https://github.com/laravel/scout/blob/7.0/... 默认使用了 chunk 了,普通的也就是上线的整体导入一次,通常可以接受。量特别大的话,想办法多个服务器分批同时导入吧。

5年前 评论

@liyu001989 好吧,也就是说同步只能是整表导入,数据变多了或者变更还是只能整表?有点不合理,要是能根据字段判断进行更新上传就好了。。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!