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:

<form method= "POST" action= "/profile">
@csrf
...
</form>
laravel forms

Method Field

HTML forms can`t 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:

<form action=”/foo/bar” method=”POST”>
@method(‘PUT’)
…..
</form> 

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:

<label for="title">Post </label>
<input id="title" type="text" class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
laravel forms 1

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

laravel forms 2

Sub-views

Blade`s @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:

<div>
@include('shared.errors')
<form>
<!-- Form Contents -->
</form>
</div> 

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

@include(‘view.name’,[‘some- => ‘data’])

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:

@includeif(‘view.name’,[‘some- => ‘data’])

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

@includewhen($boolean,‘view.name’, [‘some- => ‘data’])

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

@includeFirst([‘custom.admin],[‘some’ => ‘data’])
  • 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:

<input type=”{{ $type ?? ‘text’ }}”>

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:

use Illuminate\Support\Facades\Blade;
Blade::include(‘include.input’, ‘input’); 

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

@input([‘type’ => ‘email’])

Views for Collections

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

@each(‘view.name’,$files, ‘file’)

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.

@each(‘view.name’,$files, ‘file’, ‘view.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.


Related Topics

How to Install Git on Windows

Git Installation Git is a distributed version control system. It is used for tracking changes in source code during development. The goals include speed, data integrity, non-linear workflows, etc. Git was developed in 2005. It is free...

3 minutes read.

Laravel Data Views

Passing the Data Views The data should be an array with the key/value pairs while passing the information in the following (return view(‘greeting’, [‘name’ => ‘John’]);) manner. Inside the view, we...

4 minutes read.

Laravel Control Statements

Blade also provides convenient shortcuts for common control structure in PHP, such as conditional statements and loops. These shortcuts provide a very clean working with PHP structures, while also remain familiar to...

3 minutes read.

Laravel Templating Inheritance

Introduction: The Blade is a PHP Templating Engine. It grabs the PHP code and makes it easy for us to implement in HTML. The Blade is a simple, and powerful Templating Engine provided by...

4 minutes read.

Raw SQL Queries

Raw SQL Queries: Laravel interact with databases by the variety of database back-ends with raw SQL, the fluent query builder, and the Eloquent ORM. Laravel supports four types of databases: MySQLPostgreSQLSQLiteSQL Server Configuration The database...

3 minutes read.

Laravel Authentication

Authentication is a process of identifying user credentials. Laravel makes implementing authentication very simple. The authentication configuration file is located at config/auth.php that contains various documented options for adjusting the behavior of the authentication...

4 minutes read.

Laravel Creating Views

Creating Views Views hold the HTML served by our application, and it separates our controller/application logic with the help of presentation logic. Views are stored inside the resources/views directory. A simple view...

2 minutes read.

Laravel Advantages

Top 8 Advantages of Laravel The following advantages that Laravel offers, it is based upon designing a web application:- 1. Powerful Authentication: The Laravel PHP framework was developed with a purpose that can help...

2 minutes read.

Laravel Basic Routing

Basic Routing The basic Laravel routes accept a URI and a Closure, providing a straightforward method for defining routes: Route::get('/', function () { return view('welcome') }); E.g.: Route::get('/', function () { return "Hello Laravel"; }); Output: We...

3 minutes read.

Laravel Displaying Data

Display data that is passed to our Blade views by enclosing the variable in curly braces. The following route is given below: Route::get('greeting', function () { return view('welcome', ['name' => 'rafia']); }); We display...

2 minutes read.

Laravel Resource Controllers

Laravel resource routing assigns the “CRUD” routes to a controller with the help of single line code. For E.g., If we wish to create a controller that handles all HTTP requests “photos” stored by...

4 minutes read.

XAMPP Installation

install xampp on windows XAMPP is a free & open-source stack package which is developed by Apache, mainly it consists of Apache HTTP server, Maria DB database for scripts written in the...

2 minutes read.

Laravel Route Parameters

Required Parameters: We need to capture segments of the URI within our route. We are going to see how to pass parameters through two or many views inside the closure function. Route::get('/home',...

2 minutes read.

Laravel Controller Middleware

Controller Middleware Middleware can be assigned to the controller`s routes in our route files: Route::get(‘profile’, ‘UserController@show’) ->middleware(‘auth’); It is more convenient to specify middleware within our controller`s constructor. Using the middleware method from our controller`s constructor,...

1 minute read.

Laravel Validation

Laravel provides a different way to validate the application`s incoming data. Laravel base controller class uses a ValidatesRequests that provides an easy method to validate incoming HTTP request with many powerful validation rules. Defining the...

5 minutes read.

Laravel Eloquent Database Relationships

Eloquent Relationships Laravel: Database tables are related to one another. Eloquent manages and work with easy relationships. It supports some different types of relationship, which are as follows: One to oneOne to manyMany...

8 minutes read.

CSS Icons

CSS Icons       CSS icons are specified like a symbol or image used inside a computer interface assign to any element. CSS icons are the graphical depiction of any program or...

4 minutes read.

Laravel Uploading Files

Retrieving Uploaded Files We can access files from an Illuminate\Http\Request. By using the file method or using the dynamic properties. The file method returns an instance of the Illuminate\Http\UploadedFile class that extends the...

2 minutes read.

Laravel History and Versions

Laravel History Taylor Otwell created the Laravel; it was created as an attempt to provide an advanced alternative to the CodeIgniter framework. The CodeIgniter does not provide several features such as built-in support for...

3 minutes read.

Laravel Basic Controllers

Controllers group relate request handling logic into a single class. They are stored in the app/Http/Controllers directory. Basic Controllers Defining Controllers: The controllers extend the base controller class included with Laravel. The base class...

2 minutes read.