索引users_index不存在laravel scout

时间:2019-02-26 14:42:05

标签: php laravel-5 laravel-5.7 algolia laravel-scout

我正在尝试构建一个搜索功能,它说“ users_index”不存在,而它来自App\User.php的模型中,我不明白为什么它会抛出该错误?有人可以解释为什么会引发错误吗?

错误:索引users_index不存在

请注意scout.php已正确设置, ALGOLIA_APP_ID和ALGOLIA_SECRET'和'queue' => env('SCOUT_QUEUE', true),

web.php Route::get('index','SearchController@search')

searchcontroller.php

 namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class SearchController extends Controller
{
public function search(Request $request)
{
    if($request->has('search')){
        $users = User::search($request- 
>get('search'))->get();
    }else{
        $users = User::get();
    }
    return view('index', compact('users'));
}
}

刀片

 <body>
<div class="container">
<h1>Laravel Scout Search Tutorial</h1>
<form method="GET" action="{{ url('index') 
}}">
    <div class="row">
        <div class="col-md-6">
            <input type="text" name="search" class="form-control" placeholder="Search">
        </div>
        <div class="col-md-6">
            <button class="btn btn-info">Search</button>
        </div>
    </div>
</form>
<br/>
<table class="table table-bordered">
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
    </tr>
    @if(count($users) > 0)
        @foreach($users as $user)
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
            </tr>
        @endforeach
    @else
        <tr>
            <td colspan="3" class="text- 
danger">Result not found.</td>
        </tr>
    @endif
</table>
</div>
</body>

用户模型

namespace App;

 use Illuminate\Notifications\Notifiable;
 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Laravel\Scout\Searchable;
  use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;
use Searchable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

public function searchableAs()
{
    return 'users_index';
}

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];
}

1 个答案:

答案 0 :(得分:0)

忘记在命令行中执行此命令 php artisan scout:import "App\User" 现在工作正常