Laravel Creating Views

Facebooktwitterredditpinterestlinkedinmailby feather

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.

Creating Views

A simple view is to look something like this:

Laravel Creating Views

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

Laravel Creating View

We can easily return it using the view helper which is shown below:

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 that displayed in the view using the Blade syntax.

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

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

return view(‘admin.profile’, $data);

Determining If a View Exists

If 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:

Facebooktwitterredditpinterestlinkedinmailby feather