php - Laravel 5 AJAX 搜索
问题描述
我知道这个问题是重复的,但没有一个能解决我的问题。我有一个 AJAX 搜索,它将显示特定客户端的所有请求。
客户有很多请求,它是一对多的关系。
现在这是我的routes.php
Route::resource('client', 'ClientController',
['as' => 'encoder']);
我有一个资源路由,它接受所有 4 个 http 请求,但我的目标是GET/specificResource,id 在参数中传递。
现在在我的ClientController.php中的 show 方法中
public function show($id, Request $request)
{
$client = $this->clientRepository->with(['request' => function ($query) {
$query->orderBy('created_at', 'desc');
}])->findWithoutFail($id);
$keyword = $request->get('keyword');
if (!$keyword) {
$client_requests = AnalysisRequest::where('client_id', $client->id)
->orderBy('created_at', 'desc')->get();
} else {
$client_requests = AnalysisRequest::where('client_id', $client->id)
->OrWhere('id', 'LIKE', '%keyword%')
->OrWhere('sample_descrition', 'LIKE', '%keyword%')
->OrWhere('special_instruction', 'LIKE', '%keyword%')
->orderBy('created_at', 'desc')->get();
}
// dd($client_requests);
if (empty($client)) {
Flash::error('Client not found');
return redirect(route('encoder.client.index'));
}
return view('encoder-dashboard.client.show', compact('client_requests'))->with('client', $client);
}
现在我的ajax脚本如下。
@section('scripts')
<script>
$(document).ready(function(){
$('.searchbar').on('keyup', function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var text = $('#searchbar').val();
$.ajax({
dataType: "json",
type:"GET",
url: '{{ url('encoder/client') }}' + '/' + $('.client_id').val(),
data: {text: $('.searchbar').val()},
success: function(response) {
console.log(response);
}
});
});
});
</script>
@endsection
在我的show.blade.php
<div class="row">
<div class="col-sm-6">
<div class="form-group" id="results">
<input type="hidden" id="client_id" value="{{ $client->id }}">
<input class="form-control" id="searchbar" name="searchbar" placeholder="Search...">
</div>
</div>
</div>
@include('encoder-dashboard.client.request')
最后是request.blade.php
<!-- The timeline -->
@if (isset($client_requests) && count($client_requests) > 0)
@foreach($client_requests as $request)
<ul class="timeline timeline-inverse">
<!-- timeline time label -->
<li class="time-label">
<span class="bg-red">
{{ $request->created_at->format('M d, Y') }}
</span>
</li>
<!-- /.timeline-label -->
<!-- timeline item -->
<li>
<i class="fa fa-edit bg-blue"></i>
<div class="timeline-item">
<span class="time"><i class="fa fa-clock-o"></i> {{ $request->created_at->diffForHumans() }}</span>
<h3 class="timeline-header">Request Code: <a href="{!! route('encoder.analysis-request.show', $request->id) !!}">{{ $request->reference_no() }}</a>
@if ($request->rushable == 1)
<p style="color:red">This request is for RUSH!</p>
@else
@endif
</h3>
<div class="timeline-body">
Description: <b>{{ $request->sample_description }}</b>
<br>
Service Requested: <b>{{ $request->service->description }}</b>
<br>
Category Requested: <b>{{ $request->category->name }}</b>
<br>
Method Requested: <b>{{ $request->methodology->name }}</b>
<br>
Special Instruction: <b>{{ $request->special_instruction }}</b>
<br>
@if ($request->status == 'for_testing')
Status: <span class="label label-warning">Pending</span>
@elseif ($request->status == 'under_analyzation')
Status: <span class="label label-info">Under Analyzation</span>
@elseif ($request->status == 'finished')
Status: <span class="label label-success">Finished</span>
@endif
</div>
<div class="timeline-footer">
<a class="btn btn-primary btn-xs" href="{!! route('encoder.analysis-request.show', $request->id) !!}">Read more</a>
{{-- <a class="btn btn-danger btn-xs">Delete</a> --}}
</div>
</div>
</li>
</ul>
@endforeach
@endif
我发现了错误,问题是我在搜索输入时仍然需要按 Enter。
我收到500 内部服务器错误 我已经尝试过这个链接,但仍然给我同样的错误。这是下面的错误。
**
jquery.min.js:4 GET http://127.0.0.1:8000/encoder/client/1?keyword=4 500(内部服务器错误)发送@jquery.min.js:4 ajax@jquery.min.js: 4(匿名)@ 1:437 dispatch @ jquery.min.js:3 q.handle @ jquery.min.js:3 1:446 Uncaught ReferenceError: data is not defined at Object.error (1:446) at i ( jquery.min.js:2) 在 Object.fireWith [as rejectWith] (jquery.min.js:2) 在 A (jquery.min.js:4) 在 XMLHttpRequest。(jquery.min.js:4)
**
感谢有人可以提供帮助。提前致谢。
解决方案
请试试 -
<input class="form-control searchbar" id="searchbar" name="searchbar" placeholder="Search...">
您正在使用类标识符,但我认为您忘记在输入字段中添加类搜索栏。
推荐阅读
- mysql - SQL如何根据条件从另一列中减去一列
- cdo-climate - 位置 1 无效字符的 Cdo 操作错误!在 nc 文件中
- google-analytics - 谷歌标签管理器:点击事件仅在点击容器时触发,而不是在点击其子项时触发
- javascript - react native - 使用 numColumns 渲染大的 FlatList
- server - 运行 expressjs 或任何其他服务器的无线嵌入式服务器
- javascript - 如何使用窗口打印从 fetch API 打印图像?
- c - 在内核模块中分配物理上连续的页面
- r - 如何在 psych:mediate 中提取 ACME 的 p 值?
- javascript - 为什么分配属性会导致“错误:超出最大调用堆栈大小”错误?
- c# - Nsubstitute 根据对象属性返回不同