mercredi 28 décembre 2016

Getting values from checkboxes in Laravel 5

I'm working on a quote request form on my site and need some help with functionality. I'm trying to get the values from a set of checkboxes that I passed to the view; like so:

Controller (getQuote)

public function getQuote()
    {

      $services = array(
        'Accept donations',
        'Update website on your own',
        'E-Commerce',
        ...
      );

        return view('pages.quote')->withServices($services);

    }

Controller (postQuote)

public function postQuote(Request $request)
    {
      $this->validate($request, [
        'services' => 'required|array',
        ...
      ]);


      $data = array(
        'services' => $request->service,
        ...
      );

      $data['services_checked'] = Input::get('service');
      if(is_array($services_checked))
      {
      }

      Mail::send('emails.quote', $data, function($message) use ($data){
        $message->from($data['email']);
        $message->to('hi@sogenius.io');
        $message->subject('New quote request for ' .  $data['firstname'] . ' ' . $data['lastname']);

      });

      return redirect()->back()->with('message', "Thanks, your quote request has been sent");

    }

View

I then loop through the values in the passed array and display it to the user.

<article class="col-md-12 text-left services bx1-margin">
                

                
                <ul>
                @foreach ($services as $servicesid => $service)
                  <li>
                     --}}
                    <input id="" value="" name="service[]" type="checkbox" class="custom-control-input form-check-input">
                    <span class="custom-control-description"></span>
                    " name="services" type="checkbox" class="custom-control-input form-check-input"> --}}


                  </li>
                @endforeach
                </ul>
              </article>

When the user submits the form, how do I go about receiving the checked checkboxes?

Research

I saw this post here:How to get the values for a series of checkboxes in Laravel 4 controller (if checked) I saw a post her (stackoverflow) and it explains a setting the value to array[], I'm just not quite sure about implementation.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire