【发布时间】:2018-12-01 19:36:33
【问题描述】:
我在 ParticipantController 中有这两种方法。 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 时,所有注册都已经列在一个表格中,下面的 foreach 使用 index() 方法中的代码:
@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>
【问题讨论】:
-
使用 jquery 数据表来显示列表。它会自动处理搜索、分页等。