laravel BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::makeAllSearchable()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39582795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:28:54  来源:igfitidea点击:

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

phplaravellaravel-5.3

提问by A. Appleby

I am trying to run the command php artisan scout:import "App\User"to import user records into search driver as per documentation (Laravel 5.3 Scout Documentation). I keep getting [BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::makeAllSearchable() as an error. Why am I getting this error? I have included the searchable trait in my users controller and added the scout class to my app/config providers array, so I am struggling to see why the method doesn't exist...

我正在尝试php artisan scout:import "App\User"根据文档(Laravel 5.3 Scout 文档)运行命令将用户记录导入搜索驱动程序。我不断收到 [BadMethodCallException]
调用未定义方法 Illuminate\Database\Query\Builder::makeAllSearchable() 作为错误。为什么我收到这个错误?我已经在我的用户控制器中包含了可搜索特征,并将 scout 类添加到我的应用程序/配置提供程序数组中,所以我正在努力了解为什么该方法不存在......

回答by jakub wrona

You should not add the trait to the controller but to the model. So in your case to App\User.php

您不应该将特征添加到控制器,而是添加到模型。所以在你的情况下 App\User.php

<?php

namespace App;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Searchable;
}

回答by Reddalo

Like Jakub has said, you have to add the Searchable trait to your User model, not to the controller.

就像 Jakub 所说的那样,您必须将 Searchable 特征添加到您的 User模型中,而不是添加到控制器中。

If you're using toSearchableArray()on your model, do not forget to include the idcolumn in the array, otherwise it won't work.

如果您在toSearchableArray()模型上使用,请不要忘记将id列包含在数组中,否则它将无法工作。

You could also comment the toSearchableArray()function, import the existing users and then add it back.

您还可以评论该toSearchableArray()函数,导入现有用户,然后将其添加回来。