Working With Forms In Laravel 8

XAMPP and Visual Studio Code

Let’s Get Started

While you can use natural HTML syntax, Laravel has introduced Laravel Collective, to use for forms visit here to see the full documentation.

Laravel Collective
composer require laravelcollective/html
‘providers’ => [
Collective\Html\HtmlServiceProvider::class,
“Form” => Collective\Html\FormFacde::class,
‘Html’ => Collective\Html\HtmlFacde::class,

Using A Form

The opening and closing form tags look like this:

OPENING TAG

{!! Form::open([‘action’ =>’App\Http\Controllers\BookController@store’, ‘method’ => ‘post’, ‘enctype’ => ‘multipart/form-data’, ‘class’ => ‘container’]) !!}
<form action=”App\Http\Controllers\BookController@store” method=”POST” enctype=”multipart/form-data”>

CLOSING TAG

{!! Form::close() !!}
</form>

Explanation:

The action tells us where we’re sending the form data, usually, you have a Controller resource set up with an equivalent store function and the method tells us we’re sending with POST instead of GET, the enctype and file is if you desire to upload images/files as well. And finally, you can style it up by adding a class. You can also add a route:

[‘route’ => ‘route.name’]

Using Labels

One of the most important things that often go with form components is the label tag, telling us what a particular field is for. To use a label from Laravel Collective:

Form::label(‘email’, ‘E-Mail Address’, [‘class’ => ‘awesome’]);

Explanation:

<label for=”email”>Email Address</label>

Using Input fields

Text Inputs e.g. Name

Form::text(‘name’, ‘Jake Fisbach’, [‘class’ => ‘form-control’]);

Explanation:

<input type=”text” value=”Jake Fisbach” class=”form-control”>

Email Input

{!! Form::email(‘Email’, ‘ ‘,[‘class’ => ‘form-control’]) !!}
<input type=”email” value = ‘ ‘ class=”form-control” >

Number Input

{!! Form::number(‘number’, ‘’, [‘class’=>’form-control’]) !!}
<input type=”number” value = ‘ ‘ class=”form-control”>

Password Input

{!! Form::password(‘password’, [‘class’=>’awesome’]) !!}
<input type=”password” class=”form-control”>

Submit Button

This is by far the easiest to do. Especially if you’re using a controller with a resource with a store function because it will automatically make a POST request to it.

{!! Form::submit(‘Submit’, [‘class’ => ‘btn btn-success’]) !!}
<input type=”submit” class=”btn btn-success”>

VALIDATION

To the store function in your controller, which looks something like

public function store(Request $request) {}Go ahead and add these lines:‘name’=> ‘required’,‘email’ => ‘required’,‘number’ => ‘required’,‘password’ => ‘required’,
@if(count($errors) > 0)@foreach($errors->all() as $error)<div class=”alert-danger alert”>{{$error}}</div>@endforeach@endif@if(session(‘success’))  <div class=”alert alert-success”>{{session(‘success’)}}</div>@endif@if(session(‘error’))  <div class=”alert alert-danger”>{{session(‘error’)}}</div>@endif
@include(‘path.response’)
Error for Laravel Form
 $var = new Model;$var->name = $request->input(‘name’);
..after others,
$var->save();
return redirect(/towhereyouwant)->(‘success’, ‘Form Submitted’);

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Emmanuel Unyime

Emmanuel Unyime

I’m a Software Engineer && Technical Writer passionate about data structures and beating former “best-yet(s)”. Oh, I play Chess too!