Laravel Forms

CSRF Field

For defining an HTML form in our application, we should include a hidden CSRF token field in the form, so that the CSRF protection middleware can validate the request. We use the @csrf Blade directive to generate the token field:

laravel forms

Method Field

HTML forms cant make PUT, PATCH, or DELETE requests. We will need to add a hidden _method field for these HTTP verbs.

The @method Blade directive can create the field for us:

Validation Errors

The @error directive used to check if the Validation error messages exist for the given attribute inside an @error directive.

We can echo the $message variable to display the error message:

laravel forms 1

The create.blade.php file is stored inside the resources/views/post/create.blade.php.

laravel forms 2

Sub-views

Blades @include directive allows us to include a Blade view within another view.

All the variables that are available for the parent view will be available to the included view:

The included view will inherit all the data available in the parent view; we pass an array of extra data to the included view:

If we attempt to @include a view that does not exist, Laravel give an error.

If we would like to include a view that may or may not present, we use the @includeif directive:

If we would like to @include a view that is depending on a given Boolean condition, we use the @includewhen directive:

To include the first view, which is existing from a given array of views, we use the includeFirst directive:

  • We should avoid the usage of __DIR__ and __FILE__ constants in our views.

They will refer to the location of the cached compiled view.

Aliasing Includes

Blade includes are stored in a sub-directory, we alias them for easier access.

For example, suppose that the Blade includes which is stored at resources/views/includes/input.blade.php with the following content:

We use the include method to alias the include from includes.input to input.

It should be done in the boot method of our AppServiceProvider:

Once the include has been aliased, we provide it using the alias name as the Blade directive:

Views for Collections

We combine loops and includes into one line with Blade`s @each directive:

The first argument is the view partial to providing for each element in the array.

The second argument is the array or collection we wish to iterate over; the third argument is the variable name which will be assigned to the current iteration within the view.

For example, if we are iterating over an array of files, we want to access each file as a file variable within our view partial.

The key for the current iteration will be available as the key variable within our view partial.

We also pass the fourth argument to @each directive.

This argument determines the view that will be provided if the given array is empty.

Views are rendered via @each do not inherit the variables from the parent view.

If the child view requires these variables, we should use @foreach and @include instead.