Laravel Forms


  1. Laravel Collective
  2. Forms AJAX to Controller
  3. Debug
  4. References

Laravel Collective Forms and Html

Laravel Collective is a community organisation that adds to the original framework. Require the package from terminal, then include the Provider and Aliases to their arrays, both found in config/app.php:

'providers' => [
    // ...
    Collective\Html\HtmlServiceProvider::class,
    // ...
  ],
'aliases' => [
    // ...
      'Form' => Collective\Html\FormFacade::class,
      'Html' => Collective\Html\HtmlFacade::class,
    // ...
  ],

Usage and examples

By default the advantages of writing Form HTML in this format is, a CSRF Token is automatically generated, forms can be written using PHP code instead of HTML and there is less code to be written in general:

{{ Form::open(array('action' => 'exampleController@example', 'method' => 'post')) }}

    <div class="form-group"><!-- Username is required -->
      {{ Form::label('username', 'Username:', array('class' => 'control-label')) }}
      {{ Form::text('username', '', array('id'=>'username-id', 'class'=>'form-control', 'required')) }}
    </div>

    <div class="form-group"><!-- Email is required -->
      {{ Form::label('email', 'E-Mail Address:', array('class' => 'control-label')) }}
      {{ Form::text('email', '', array('id'=>'email-id', 'class'=>'form-control', 'required')) }}
    </div>

    <div class="form-group"><!-- First Radio is selected -->
      {{ Form::label('radio_name', 'Radio Button:', array('class' => 'control-label')) }}
      {{ Form::radio('radio_name', 'radio_value1', true, array('id'=>'radio-id', 'class'=>'form-control')) }}
      {{ Form::radio('radio_name', 'radio_value2', false, array('id'=>'radio-id', 'class'=>'form-control')) }}
    </div>

    <div class="form-group">
      {{ Form::submit('Submit') }}
    </div>

{{ Form::close() }}

Laravel Ajax Call to Function in Controller

The resource Controller provides many common methods to create, read, update or delete (CRUD). AJAX requests are particularly suited to match the method type with the function type inside the Controller (refer to using php artisan route:list to view the url and method types).

Usage and examples

If you are working with AJAX and sending to a Controller function, remember to include a JSON response at the end of the function:

// END OF CONTROLLER FUNCTION
return response()->json([
  'request' => $request,
  'success' => true
]);
// POST METHOD
$.ajax({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  },
  type: 'POST',
  url: '/example/' + id,
  data: { id : id },
  dataType: 'json',
});


// GET METHOD
$.ajax({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  },
  type: 'GET',
  url: '/example',
  dataType: 'json',
});


// PUT METHOD
$.ajax({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  },
  type: 'PUT',
  url: '/example/' + id,
  data: {'id': id},
  dataType: 'json',
});


// DELETE METHOD
$.ajax({
  headers: {
    'X-CSRF-TOKEN': $("meta[name=csrf-token]").attr('content')
  },
  type: 'DELETE',
  url: '/example/' + id,
  data: {"id" : id},
  dataType: 'json',
});

Debug

  1. AJAX NOT working
  1. Not reaching Controller

References