laravel
  1. laravel-forms

Forms - Laravel Blade Template

In Laravel, Blade is a templating engine that allows developers to create reusable and dynamic web pages with minimal effort. One of the most common use cases for Blade templates is creating forms. In this article, we'll explore how to use Blade templates to create forms in Laravel.

Syntax

<form method="POST" action="{{ route('route_name') }}">
    @csrf
    // Form fields go here
</form>

Example

<form method="POST" action="{{ route('submit_form') }}">
    @csrf
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>

    <label for="email">Email:</label>
    <input type="email" id="email" name="email" required>

    <label for="message">Message:</label>
    <textarea id="message" name="message" required></textarea>

    <button type="submit">Submit</button>
</form>

Output

The code above will render a form with three input fields and a submit button. When the form is submitted, the data will be sent to the server using the POST request method.

Explanation

The form element defines a form that will be used to collect user input. The method attribute specifies the type of request that will be used to submit the form. In this case, we are using the POST method.

The action attribute specifies the URL that the form data will be sent to when the submit button is clicked. This URL can be generated using the route function.

The @csrf directive adds a hidden field to the form that includes a CSRF token. This token is used to verify that the form data was submitted from your own application, and not from another site.

Each form field is defined using an HTML label element and an appropriate input field. The name attribute of each input field corresponds to the key that will be used to access the value of the field when the form is submitted.

When the form is submitted, Laravel will automatically parse the data and make it available in the $request object.

Use

Blade templates provide an easy and consistent way to create forms in Laravel. Forms can be used for a wide range of purposes, such as collecting user information, posting comments, and creating orders.

Important Points

  • The form element defines a form that will be used to collect user input.
  • The method attribute specifies the type of request that will be used to submit the form.
  • The action attribute specifies the URL that the form data will be sent to when the submit button is clicked.
  • The @csrf directive adds a hidden field to the form that includes a CSRF token.
  • Each form field is defined using an HTML label element and an appropriate input field.
  • Laravel will automatically parse the data and make it available in the $request object when the form is submitted.

Summary

In this article, we learned how to use Blade templates to create forms in Laravel. We covered the syntax for creating forms, how to access the form data in Laravel, and some common use cases for forms. Forms are a powerful tool for collecting user input and interacting with the server, and Blade templates make it easy to create forms in Laravel.

Published on: