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.

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:

Route::get(‘/’, function ( )
Return view(‘greeting’, [‘name’ => ‘John’]);

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.

use Illuminate\Support\Facades\View;
if (View::exists('emails.customer')) {

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:

return view( )->first([‘custom.admin’, ‘admin’], $data);

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

use Illuminate\Support\Facades\View;
return View::first(['custom.admin', 'admin'], $data);