mercredi 24 février 2016

How to iterate through a check-box array and query from each element

I have a set of check boxes allowing the user to select events. The value of each box is the id of the event in its respective table. I am able to grab the values of the boxes that are checked, but I now need to iterate through each value and query for that event id in the database and display that info on the next view. Here is my view:

@foreach ($events as $events)
  <tr>
    <td>{{$events->Name}}</td>
    <td>{{$events->Summary}}</td>
    <td class="select_event">{!! Form::checkbox('event_name[]', $events->Event_ID) !!}</td>
  </tr>
@endforeach

And the method that supplies the variables to that view:

public function index() {
  $events = Events::all();
  $user = Auth::user()->name;
  $id = 1;
  return view('Delta.Index')->with([
    'user'    => $user,
    'events'  => $events,
    'id'      => $id,
  ]);
}

and the method that I need to use to query from the database:

public function generate(Requests\Generate $request) {
  //This grabs the values of all of the selected check boxes which happens to be the event id's
  $event_id = implode(",", $request->input('event_name')); 
  //This attempts to query from the database based on the event id's that were selected.
  $event = \App\Events::where('event_id', $event_id)->get(); 
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire