text 实时搜索laravel searchable和vue
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 实时搜索laravel searchable和vue相关的知识,希望对你有一定的参考价值。
composer require spatie/laravel-searchable
<?phpnamespace App;use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;
use Illuminate\Database\Eloquent\Model;
class Member extends Model implements Searchable
{
public function getSearchResult(): SearchResult
{
$url = route('members.show', $this->id);
return new SearchResult($this, $this->full_name, $url);
}
}
<?phpnamespace App\Http\Controllers;use App\Member;
use Spatie\Searchable\Search;
use Illuminate\Http\Request;
class MemberSearchController extends Controller
{
public function index(Request $request)
{
$results = (new Search())
->registerModel(Member::class, ['first_name', 'last_name'])
->search($request->input('query'));
return response()->json($results);
}
}
Route::get('members/search', 'MemberSearchController@index');
<template>
<div>
<input type="text" placeholder="Search" v-model="query">
<ul v-if="results.length > 0 && query">
<li v-for="result in results.slice(0,10)" :key="result.id">
<a :href="result.url">
<div v-text="result.title"></div>
</a>
</li>
</ul>
</div>
</template>
data() {
return {
query: null,
results: []
};
},
watch: {
query(after, before) {
this.searchMembers();
}
},
methods: {
searchMembers() {
axios.get('members/search', { params: { query: this.query } })
.then(response => this.results = response.data)
.catch(error => {});
}
}
https://medium.com/@matttonks11/creating-an-instant-search-bar-with-laravel-vue-3127d1bc57b3
以上是关于text 实时搜索laravel searchable和vue的主要内容,如果未能解决你的问题,请参考以下文章
使用 AJAX 的多个实时搜索 Laravel
Laravel Eloquent 执行实时搜索
具有多个模型的 Laravel ajax 实时搜索查询
Vue JS 对分页 Laravel 结果的实时客户端搜索过滤器
php 使用实时结果查看Laravel搜索文件:https://www.cloudways.com/blog/live-search-laravel-ajax/
laravel - 资源实时刷新