我在ParticipantController中有这2个方法。 index()显示会议中所有注册的一些信息。然后search()用于视图中的搜索表单,以仅在表中显示结果,其中进行注册的用户的名称类似于该用户在搜索表单中引入的名称:
class ParticipantController extends Controller
{
public function index($id){
$conference = Conference::find($id);
$conference->load('registrations.participants.answers.question');
return view('participants.index')->with('conference', $conference);
}
public function search($id, Request $request)
{
$name = $request->get('participant');
$conference = Conference::findOrFail($id);
$searchInfo = $conference->load([
'registrations.participants' => function ($query) use ($name) {
return $query->where('name', 'like', '%' . $name . '%');
},
'registrations.participants.answers.question'
]);
return view('participants.index', compact('conference'));
}
}
此方法的路由:
Route::get('conference/edit/{id}/participants', [ 'uses' => 'ParticipantController@index', 'as' => 'participants.index']);
Route::get('conference/edit/{id}/participants/search', [
'uses' => 'ParticipantController@search',
'as' => 'participants.search'
]);
我的疑问是,在用户在搜索表单中输入一些数据并单击“搜索”后,如何在视图中显示搜索结果。当用户访问参与者/index.blade.php时,使用index()方法中的代码,所有注册都已在带有foreach的表中列出:
@foreach($conference->registrations as $registration)
<tr>
<td>{{$registration->customer->name}} {{$registration->customer->surname}}</td>
<td>{{ $registration->status === 'C' ? 'Complete' : 'Incomplete' }}</td>
<td><a data-regid="{{$registration->id}}"> More details</a></td>
</tr>
@endforeach
工作正常,该表显示所有注册的详细信息,例如:
User that did the registration Quantity Status Details
Jake K 2 Complete Show
...
但是在此视图中,还存在搜索表单,我的疑问是,当用户在搜索表单中引入名称并单击“搜索”时,如何在表中显示搜索结果。
视图中搜索表单的HTML:
<form action="{{ route('participants.search', ['id' => $conference->id] )}}" method="get">
<div class="form-row">
<div class="form-group col col-md-6">
<label for="participant">Search</label>
<div class="input-group">
<input type='text' name="participant" class="form-control" placeholder="Name" />
</div>
</div>
</div>
</form>
答案 0 :(得分:1)
更改您的搜索操作
public function search($id, Request $request)
{
$name = $request->get('participant');
$conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
return $query->where('name', 'like', '%' . $name . '%');
},'registrations.participants.answers.question'])->findOrFail($id);
return view('participants.index', compact('conference'));
}
现在,如果您仔细查看索引和搜索具有相同的返回类型conference
,只是区别在于我们已经过滤了参与者。
OR
您也可以在 index 操作中添加搜索功能
public function index($id, Request $request){
$name = $request->get('participant');
if($name){
$conference = Conference::with(['registrations.participants' => function() ($query) use ($name){
return $query->where('name', 'like', '%' . $name . '%');
},'registrations.participants.answers.question'])->findOrFail($id);
}else{
$conference = Conference::with('registrations.participants.answers.question')->findOrFail($id);
}
return view('participants.index')->with('conference', $conference);
}
答案 1 :(得分:0)
您可以使用数据表 链接:https://github.com/yajra/laravel-datatables
检查。这将解决您的问题。
答案 2 :(得分:0)