I have this 2 methods in the ParticipantController. The index() show some info of all registrations in a conference. Then the search() is for a search form in the view, to show only in the table the results where the user that did the registration has the name like the name introduced by the user in the search form:
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'));
}
}
The routes that I have for this methods:
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'
]);
My doubt is how to show the search results in the view after the user enter some data in the search form and click "Search". When the user acesses the participants/index.blade.php all registrations are already listed in a table with the foreach below using the code in the index() method:
@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
And its working fine the table show the details of all registrations like:
User that did the registration Quantity Status Details
Jake K 2 Complete Show
...
But in this view there is also the search form, my doubt is how to show in the table the search results when the user introduces a name in the search form and click "Search".
HTML of the search form in the view:
<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>