Laravel 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.

Laravel Creating Views

A simple view looks something like this:

Laravel Creating Views 1

Since, the view is stored at the resources/views/greeting.blade.php.

Laravel Creating Views 2

We can easily return it using the view helper like this:

The first argument passed to the view corresponds to the name of the view file in the resource/views directory.

The second argument is an array of data that should be available to the view.

For this case, we are going to pass the name variable which is displayed in the view using the Blade syntax.

Views are also nested within the sub-directories of the resources/views directory. “Dot” notation can be used for reference to the nested views.

If our view is stored at the resources/views/admin/profile.blade.php, we use the reference, like:

Determining if a View Exists

When we need to determine if a view exists, we can use the View façade. The exists() method is used to return true if the view exists.

Creating the First Available View

By using the first method, we can easily create the first view, which exists in an array of views. It is beneficial for our application or packages that allow views to be customized:

We can also call this method with the help of View facade: