
Laravel Forms
Laravel provides a convenient way to create and handle HTML forms in your web applications. You can easily generate form elements, validate user input, and handle form submissions using Laravel’s built-in features. Here’s a step-by-step guide to working with forms in Laravel:
1. Creating a Form:
To create an HTML form in Laravel, you typically use the Form
facade, which provides methods for generating form elements. Laravel’s Blade templating engine makes it easy to include form elements in your views. For example, to create a simple contact form:
<form method="POST" action="/contact">
@csrf
<label for="name">Name:</label>
<input type="text" name="name" id="name">
<label for="email">Email:</label>
<input type="email" name="email" id="email">
<button type="submit">Submit</button>
</form>
In this example, we use @csrf
to include a CSRF token for security purposes. The action
attribute specifies where the form data should be submitted.
2. Handling Form Submissions:
In your controller, you can handle form submissions by defining a method to process the submitted data. In Laravel, you can access form data using the Request
object. Here’s an example of a controller method for processing the contact form:
use Illuminate\Http\Request;
public function contact(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
]);
// Process the form data, send emails, save to the database, etc.
return redirect('/contact')->with('success', 'Message sent successfully!');
}
In this method, we use the validate
method to validate the incoming form data based on defined validation rules. If validation fails, Laravel will automatically redirect back to the form with error messages.
3. Displaying Validation Errors:
To display validation errors in your view, you can use the @error
directive and the withErrors
method in your controller. For example:
<label for="name">Name:</label>
<input type="text" name="name" id="name">
@error('name')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
<label for="email">Email:</label>
<input type="email" name="email" id="email">
@error('email')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
In your controller, you can pass the validation errors to the view using the withErrors
method:
return redirect('/contact')
->withErrors($validator)
->withInput();
4. Old Input Values:
Laravel automatically re-populates form fields with old input values when there are validation errors. You can use the old
function in your view to access these values:
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="{{ old('name') }}">
<label for="email">Email:</label>
<input type="email" name="email" id="email" value="{{ old('email') }}">
5. Flashing Success Messages:
After processing the form, you can use the with
method to flash success messages to the next request:
return redirect('/contact')->with('success', 'Message sent successfully!');
Then, you can display the success message in your view:
@if (session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
This is a basic overview of working with forms in Laravel. Laravel provides many more features for handling forms, such as file uploads, custom form requests, and more. You can explore the Laravel documentation for in-depth information on working with forms: https://laravel.com/docs/8.x/requests