0
votes

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>
3
use jquery data-tables for the list to display. it will auotmatically takes care of search, pagination etc..sree

3 Answers

1
votes

Change you search action like this

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'));
}

Now if you look carefully both index and search has the same return type conference only the difference is in search we have filtered participants.

OR

you can just add the search functionality in index action too

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);
}
0
votes

You can use datatables Link : https://github.com/yajra/laravel-datatables

Check . this will solve your problem .

0
votes